js把多维数组的子集数组全部遍历成同级

753 ℃

1、默认数据

var arr1 = [{
	color1: "#e35752",
	color2: "#fb995f",
	data:[22223286, 6919184, 4227231, 985484, 242569],
	name: "超声",
	type: ["门诊", "住院", "体检", "急诊", "外院"]
},{
	color1: "#e35752",
	color2: "#fb995f",
	data:[22223286, 6919184, 4227231, 985484, 242569],
	name: "CT",
	type: ["门诊", "住院", "体检", "急诊", "外院"]
}]

2、遍历方法

function getDataArr(data) {
	var resData = []
	data.forEach(function(item, index) {
		if(item.data && item.data.length>0){
			var _d = item.data
			var _t = item.type
			_d.forEach(function(ditem,index){
				var _obj = JSON.parse((JSON.stringify(item)))
				_obj.data = ditem
				_obj.type = _t[index]
				resData.push(_obj)
			})
		}
	})
	return resData
}

3、执行方法

getDataArr(arr1)

4、返回结果

0: {color1: "#e35752", color2: "#fb995f", data: 22223286, name: "超声", type: "门诊"}
1: {color1: "#e35752", color2: "#fb995f", data: 6919184, name: "超声", type: "住院"}
2: {color1: "#e35752", color2: "#fb995f", data: 4227231, name: "超声", type: "体检"}
3: {color1: "#e35752", color2: "#fb995f", data: 985484, name: "超声", type: "急诊"}
4: {color1: "#e35752", color2: "#fb995f", data: 242569, name: "超声", type: "外院"}
5: {color1: "#e35752", color2: "#fb995f", data: 22223286, name: "CT", type: "门诊"}
6: {color1: "#e35752", color2: "#fb995f", data: 6919184, name: "CT", type: "住院"}
7: {color1: "#e35752", color2: "#fb995f", data: 4227231, name: "CT", type: "体检"}
8: {color1: "#e35752", color2: "#fb995f", data: 985484, name: "CT", type: "急诊"}
9: {color1: "#e35752", color2: "#fb995f", data: 242569, name: "CT", type: "外院"}

js实现对象for循环遍历赋值

js简单介绍4种数组遍历方法

js方法实现对象转url参数

php如何去掉二维数组中的key

js同数组去重

标签: 多维数组, 对象遍历

上面是“js把多维数组的子集数组全部遍历成同级”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_1443.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

谷歌浏览器不预览直接打印解决方法
帝国cms教程灵动索引标签调用有标题图片的信息
解密AJAX参数:深入了解常用参数及其作用
uniapp语法如何判断当前设置是苹果ios还是安卓android
javascript数组内根据id大小排序