4

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",  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'll alert the element and its value. Example id_request "003" New comment here .....

2
  • What output you expecting? where it is id_request "003" ? Commented Aug 21, 2015 at 4:51
  • I will create an alert, that "New comment on request 003" by js Commented Aug 21, 2015 at 6:23

3 Answers 3

2

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']);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.