数据处理,foreach循环应用

需求:

 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 原始数据

数据处理,foreach循环应用_第1张图片

endArr 输出数据

数据处理,foreach循环应用_第2张图片

备注

 开发项目中遇到的问题,随笔。

你可能感兴趣的:(Web前端,javaScript)