数组间的比较过滤/JS取出两个数组相同元素或者不同元素

取出两个数组相同元素,组成一个新的数组对象

let data = [ 
    {name: "name一",isSelected: false,value: 111},                                                                
    {name: "name二",isSelected: false,value: 222},
    {name: "name三",isSelected: false,value: 333},                                                                                                                            
    {name: "name四",isSelected: false,value: 444}
];
let arr1 = [ 
{  isHas: true ,                                                                                
   id: 111,                                                                                                      
   title: "no image11"                                                                                                   
},{
   isHas: false ,                                                                                
   id: 222,                                                                                                      
   title: "no image22"                                                                                                   
},{ 
   isHas: true ,                                                                                
   id: 333,                                                                                                      
   title: "no image33"                                                                                                   
}];
let newArr = [];
let isExit;
    for (let i = 0; i < arr1.length; i++) {
        for (let j = 0; j < data.length; j++) {
            if(data[j].value === arr1[i].id){
                isExit = true;
            }
        }
        isExit  && newArr.push(arr1[i])
 
    }
    
console.log(newArr)

数组间的比较过滤

let data = [ 
 {name: "name一",isSelected: false,value: 111},                                                                
 {name: "name二",isSelected: false,value: 222},                                                                                                                        
 {name: "name三",isSelected: false,value: 333},                                                                                                                        
 {name: "name四",isSelected: false,value: 444}                                                            ];
let arr1 = [ {  isHas: true ,                                                                                
                id: 111,                                                                                                      
                title: "no image11"                                                                                                   
             },{  isHas: false ,                                                                                
                id: 222,                                                                                                      
                title: "no image22"                                                                                                   
             },{ isHas: true ,                                                                                
                id: 333,                                                                                                      
                title: "no image33"                                                                                                   
             }];
let result = [];
let isExist= false;
for(var i = 0; i < data.length; i++){
    var obj = data[i];
    var num = obj.value;
    isExist = false;
    for(var j = 0; j < arr1.length; j++){
        var aj = arr1[j];
        var n = aj.id;
        if(aj.isHas === true && n === num){
            isExist = true;
            break;
        }
    }
    isExist&&result.push(obj);
}
console.log('yannan',result);

 

posted @ 2021-06-03 15:34  Running°つ  阅读(2206)  评论(0编辑  收藏  举报