陣列匹配-連線

青呢喃發表於2018-09-05
        var arr=[
            {id:1,name:'11'},
            {id:2,name:'11'},
        ]
        var brr =[
            {id:1,type:'11'},
            {id:2,type:'11'}
        ]
        function concatArr(arr,brr){
            var newArr =arr.concat(brr);
            var has ={},res=[];
            for(let i = 0 ;i<newArr.length;i++){
                var item= newArr[i];
                console.log(item);
                if(!has[item.id]){
                    res.push({
                        ...item
                    })
                    has[item.id] = item;
                }else{
                    for(let j=0;j<res.length;j++){
                        var se= res[j];
                        if(se.id == item.id){
                            res[j] ={...se,...item}
                            break
                        }
                    }
                }
            }
            return res;
        }
複製程式碼

相關文章