如何将一个数组对象的children转成一维数组?

[{

"opentarget": "",

"children": [{

"opentarget": "",

"smallicon": "Menu",

"label": "页面装修",

"id": "a011cd42-8eeb-41bd-ae26-e91f327d647e",

"url": "/views/entity/list?entityname=app_page"

}, {

"opentarget": "",

"smallicon": "Menu",

"label": "功能列表",

"id": "2f3d3107-1856-452f-8aee-25f2c8561888",

"url": "/views/entity/list?queryviewid=dd879dca-4626-4aa7-97c1-6a102b4edff4"

}],

"smallicon": "Finished",

"label": "小程序管理",

"id": "2aa3ea3c-cd53-4ba7-925a-0eeb936e250c",

"url": ""

}, {

"opentarget": "",

"children": [{

"opentarget": "",

"smallicon": "List",

"label": "企业列表",

"id": "584d2162-34b6-4216-8cf4-c72a3c5e7092",

"url": "/views/entity/list?queryviewid=2410d072-19cf-4cd6-8fbc-2382ac5f6875"

}, {

"opentarget": "",

"smallicon": "Menu",

"label": "服务分类",

"id": "126e0bed-17ac-4344-83dd-61f0dd028aba",

"url": "/views//entity/list?queryviewid=7d84a50a-2a3f-4463-9033-ffdf3afa922e"

}, {

"opentarget": "",

"smallicon": "Menu",

"label": "进驻分类",

"id": "7227f1f0-a970-4282-b7eb-65c97ed94efa",

"url": "/views/entity/list?queryviewid=c4dd2ac1-4b5d-4fd3-8027-7f5f1bcb960a"

}, {

"opentarget": "",

"smallicon": "Menu",

"label": "企业服务",

"id": "c4fac47d-f1ce-41d7-bbdd-d4adc7af1d21",

"url": "/views/entity/list?queryviewid=3baf5e54-25c5-4a7f-b5a2-72756114bc3f"

}, {

"opentarget": "",

"smallicon": "Menu",

"label": "企业进驻",

"id": "260062b4-7784-47e2-9e31-83d7768daffa",

"url": "/views/entity/list?queryviewid=4cadc5d6-785d-416a-9f2b-b5d09f8590ce"

}, {

"opentarget": "",

"smallicon": "Menu",

"label": "企业社团",

"id": "e3a7b696-e1b2-4fe3-ab49-ab013afa1a74",

"url": "/views/entity/list?queryviewid=88c30817-3043-4390-8dd6-98a5f2e7a7cc"

}],

"smallicon": "DataLine",

"label": "企业管理",

"id": "b7f0e154-27ed-44e7-941b-8ed6372c2658",

"url": ""

}, {

"opentarget": "",

"children": [{

"opentarget": "",

"smallicon": "Menu",

"label": "商品分类",

"id": "9b6718bb-2541-468c-a2c2-5428dc133734",

"url": "/views/entity/list?queryviewid=3278a273-20a9-4007-af66-1b39f2e13d30"

}, {

"opentarget": "",

"smallicon": "Menu",

"label": "商品列表",

"id": "c8f219b7-540a-4a6a-aa5b-3a1b5eaaa4bc",

"url": "/views//entity/list?queryviewid=0e83a0fa-be13-4d15-8168-2c7c6ff2ff4c"

}],

"smallicon": "Menu",

"label": "商品管理",

"id": "bba49ee3-e97e-4e49-92f6-e8d2530c40ea",

"url": ""

}]

我想把这个数组里面的children,和最外层的数组合并成一个一维数组,可能children里面对象数组还包含有children 怎么实现呢?


回答:

let newArr = arr.flatMap(v => v.children)


回答:

var flatArray = arr => arr.reduce((res,v) => res.concat(v, flatArray(v.children||[])), [])


回答:

首先,如果children底下还会有children的话,需要一个思路,做递归。没有的话,直接flat就好了。

function (target){

let result = []

function flatChild(target){

if(Array.IsArray(target))

target.forEach(flatChild)

else

result.push(target.children)

}

return result

}

以上是 如何将一个数组对象的children转成一维数组? 的全部内容, 来源链接: utcz.com/p/933423.html

回到顶部