把多维数组children降维到平级

2024年07月29日 建站教程

//把多维数组children降维到平级

1、数据

let data = [{
	hosp:'北京一院',
	type:'门诊',
	children:[
		{name1:1, num1:11},
		{name2:22,num2:23},
		{name3:32,num3:33},
		{name4:42,num4:43}
	]
},{
	hosp:'上海协和医院',
	type:'住院',
	children:[
		{name1:"a", num1:11},
		{name2:"a2",num2:23},
		{name3:"a3",num3:33},
		{name4:"a4",num4:43}
	]
}]

2、方法

let arr1 = []
let arr2 = []
let arr3 = []
data.forEach(item=>{
	arr1.push({
		hosp:item.hosp,
		type:item.type,
	})
	arr2.push(Object.assign({},...item.children))
	arr3 = arr1.map((item, index) => {
		return { ...item, ...arr2[index] };
	});
})

3、结果

console.log('结果输出', arr3)
[{
	hosp: "北京一院",
	name1: 1,
	name2: 22,
	name3: 32,
	name4: 42,
	num1: 11,
	num2: 23,
	num3: 33,
	num4: 43,
	type: "门诊"
},{
	hosp: "上海协和医院",
	name1: "a",
	name2: "a2",
	name3: "a3",
	name4: "a4",
	num1: 11,
	num2: 23,
	num3: 33,
	num4: 43,
	type: "住院"
}]

本文链接:http://so.lmcjl.com/news/9432/

展开阅读全文
相关内容