数据处理,foreach循环应用
程序员文章站
2022-05-25 17:06:32
...
需求:
Array数组中每一项的name值与数组的arr属性中每一个bizName值相等,即arr中的bizName值应该全为'餐厅'或便利店,foreach循环实现
let arr = [
{
name: '餐厅',
arr: [
{bizName: '111', certId: '', certName: '', certType: '', createTime: ''},
{bizName: '356', certId: '', certName: '', certType: '', createTime: ''},
{bizName: '357', certId: '', certName: '', certType: '', createTime: ''},
{bizName: '359', certId: '', certName: '', certType: '', createTime: ''}
]
},
{
name: '便利店',
arr: [
{bizName: '111', certId: '', certName: '', certType: '', createTime: ''},
{bizName: '356', certId: '', certName: '', certType: '', createTime: ''},
{bizName: '357', certId: '', certName: '', certType: '', createTime: ''},
{bizName: '359', certId: '', certName: '', certType: '', createTime: ''}
]
}
];
console.log(arr);
//定义函数
function muFunction( arr ) {
let endArr = [];
arr.forEach(obj => {
obj.arr.forEach(item => {
item.bizName = obj.name;
});
})
arr.forEach(index => {
endArr = endArr.concat(index.arr);
})
console.log(endArr);
return endArr;
}
//函数调用
muFunction(arr);
输出结果
arr 原始数据
endArr 输出数据
备注
开发项目中遇到的问题,随笔。
上一篇: 利用外键关系实现多表关联更新
下一篇: Mybatis批量更新出错问题