js比较两个对象数组,取出不同的值

2018-06-03  本文已影响0人  飞鱼_JS

方法一

var array1 = [ {"Num": "A "  },{"Num": "B" }];
var array2 = [ {"Num": "A ","Name": "t1 " }, {"Num": "B","Name": "t2"}, {"Num": "C " ,"Name": "t3 "}];
var result = [];
for(var i = 0; i < array2.length; i++){
    var obj = array2[i];
    var num = obj.Num;
    var isExist = false;
    for(var j = 0; j < array1.length; j++){
        var aj = array1[j];
        var n = aj.Num;
        if(n == num){
            isExist = true;
            break;
        }
    }
    if(!isExist){
        result.push(obj);
    }
}
console.log(result);
上一篇下一篇

猜你喜欢

热点阅读