在javascript中在两个不同的数组对象中查找具有不同值的元素

时间:2015-08-21 04:42:53

标签: javascript

如何在两个不同的数组中找到具有不同值的元素?

First Array

[Object { id_request="009",  comment_bapak="First Comment"}, 
 Object { id_request="010",  comment_bapak="Second Comment"}, 
 Object { id_request="012",  comment_bapak=null}
]

第二个数组

[Object { id_request="009",  comment_bapak="First Comment"}, 
 Object { id_request="010",  comment_bapak="Second Comment"}, 
 Object { id_request="012",  comment_bapak="New comment here ..... "}
]

我会提醒元素及其价值。示例id_request" 003" New comment here .....

3 个答案:

答案 0 :(得分:2)

过滤方法approch jsfiddle

var diffItems = function (firstAr, secAr) {
    return firstAr.filter(function (fArItm) {
        return secAr.filter(function (sArItm) {
            return fArItm.comment_bapak === sArItm.comment_bapak;
        }).length === 0;
    });
};

var arr2 = [{
    id_request: "009",
    comment_bapak: "First Comment"
}, {
    id_request: "010",
    comment_bapak: "Second Comment"
}, {
    id_request: "012",
    comment_bapak: null
}];

var arr1 = [{
    id_request: "009",
    comment_bapak: "First Comment"
}, {
    id_request: "010",
    comment_bapak: "Second Comment"
}, {
    id_request: "012",
    comment_bapak: "New comment here ..... "
}];

console.log(diffItems(arr1, arr2)[0]['comment_bapak']);

答案 1 :(得分:2)

减少复杂性的时间总是很糟糕。

如果id和目标attr已修复(在您的情况下为id_requestcomment_bapak),则可以使用对象将第一个列表转换为map

然后对于第二个列表中的每个项目,您只需要使用地图来获取第一个列表中的相关项目,然后进行比较。

时间复杂度将变为O(m + n)而不是O(m * n)

var first = [{ id_request:"009",  comment_bapak:"First Comment"}, 
 { id_request:"010",  comment_bapak:"Second Comment"}, 
 { id_request:"012",  comment_bapak:null}
];

var second = [{ id_request:"009",  comment_bapak:"First Comment"}, 
 { id_request:"010",  comment_bapak:"Second Comment"}, 
 { id_request:"012",  comment_bapak:"New comment here ..... "}
];

var difference = function(list1, list2, id, attr) {
  var map = {};
  
  // Create map.
  list1.forEach(function(item) {
    map[item[id]] = item;
  });
  
  // Find diff.
  return list2.filter(function(item) {
    var target = map[item[id]];
    // Return if the item is not exist in first, or the target attr is different.
    return (typeof target === 'undefined' || item[attr] !== target[attr]);
  });
}

var diffs = difference(first, second, 'id_request', 'comment_bapak');
console.log(diffs);
console.log(diffs[0].comment_bapak);

答案 2 :(得分:1)

您可以通过执行嵌套filter找到两个数组的差异,只返回项b中未包含在项b中的项。例如:

var first = [{ id_request:"009",  comment_bapak:"First Comment"}, 
 { id_request:"010",  comment_bapak:"Second Comment"}, 
 { id_request:"012",  comment_bapak:null}
];

var second = [{ id_request:"009",  comment_bapak:"First Comment"}, 
 { id_request:"010",  comment_bapak:"Second Comment"}, 
 { id_request:"012",  comment_bapak:"New comment here ..... "}
];

// returns an array containing the unique objects
var difference = function(arr1, arr2) {
   return arr2.filter(function(item) {
     // filter all the matches and return the negation (0 matches = true = keep in array)
     return !arr1.filter(function(firstItem) {
        // compare comment_bapak
        return firstItem.comment_bapak == item.comment_bapak;
     }).length;
   });
 };

var differentItems = difference(first, second);

alert(differentItems[0].comment_bapak);