Find elements with different values ​​in two different array objects in javascript

How to find elements with different values ​​in two different arrays?

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}
]

Second array

[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 ..... "}
]

I am warning the element and its meaning. Example id_request"003"New comment here .....

+4
source share
3 answers

approch jsfiddle filter method

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']);
+2
source

It is always goog to reduce complexity time.

If the value idand target are attrfixed (in your case id_requestand comment_bapak), you can simply use the object to convert the first list to 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

You can find the difference in the two arrays by doing nested filter, returning only elements of element a that are not contained in element 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 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);
Run code
+1
source

All Articles