Skip to content Skip to sidebar Skip to footer

Find The Elements With Different Values In Two Different Array Objects In Javascript

How to find elements with different values in two different arrays like this? First Array [Object { id_request='009', comment_bapak='First Comment'}, Object { id_request='010',

Solution 1:

filter method 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']);

Solution 2:

It's always goog to reduce the time Complexity.

If the id and target attr is fixed(in your case, which is id_request and comment_bapak), you can just use an object to convert first list to map.

Then for each item in second list, you just need to use the map the to get the related item in first list, then compare them.

The time Complexity would become O(m + n) instead of 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);

Solution 3:

You can find the difference of two arrays by performing a nested filter, returning only the items of item a that are not contained within item b. For instance:

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 objectsvar 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_bapakreturn firstItem.comment_bapak == item.comment_bapak;
     }).length;
   });
 };

var differentItems = difference(first, second);

alert(differentItems[0].comment_bapak);

Post a Comment for "Find The Elements With Different Values In Two Different Array Objects In Javascript"