在JavaScript中查找两个不同数组对象中值不同的元素。

4
如何在两个不同的数组中查找不同值的元素?例如这样:

第一个数组

[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" 这里是新评论......


你期望的输出是什么?id_request在哪里是“003”? - RIYAJ KHAN
我将通过js创建一个警报,内容为“请求003有新评论”。 - Fadly Dzil
3个回答

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

过滤方法的实现 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

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

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);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接