Skip to content Skip to sidebar Skip to footer

Compare Two Multidimensional Arrays In Javascript

I have two arrays: var array_old = [{id:'5436', title:'I Like you boy'}, {id:'5437', title:'Hello how are you'}]; var array_new = [{id:'5436', title:'I Like you boy'}, {id:'1132',

Solution 1:

http://jsfiddle.net/tppiotrowski/VHb3Q/2/

var array_old = [{
    id: "5436",
    title: "I Like you boy"},
{
    id: "5437",
    title: "Hello how are you"}];
var array_new = [{
    id: "5436",
    title: "I Like you boy"},
{
    id: "1132",
    title: "I'm fine"}];

$.each(array_old, function(old_index, old_obj) {
    var old_id = old_obj['id'];
    var found = false;
    $.each(array_new, function(new_index, new_obj) {
        if (new_obj['id'] == old_id) {
            found = true;
        }
    });
    if (!found) {
        alert(old_id + " does not exist in array_new");
    }
});​

Solution 2:

I found a way myself, but I don't know if this is the best way to do it:

var array_old = [{id: "5436",title: "I Like you boy"},{id: "5437",title: "Hello how are you"},{id: "5438",title: "Hello how are you2"}];
var array_new = [{id: "5436",title: "I Like you boy"},{id: "1132",title: "I'm fine"}];

$.each(array_old, function(id, array){

    found = 0;

    $.each(array_new, function(id2, array2) {

        if(array['id']==array2['id'])
        {
            found++;
        }

    });

    if(found==0)
    {
        alert(array['id']+' does not exist in array_new');
    }

});

http://jsfiddle.net/FAb3k/2/

Solution 3:

Depends on how big your arrays are - you might want to use a more performant solution.

  • The easiest solution (which both you and @Tebb found) has Θ(n*m)
  • If you would optimize this a bit (breaking out if you did [not] found the element - see @gonchuki), you are still at O(n*m)
  • You could assume that both arrays are in the same order, and run only one loop: O(min(n,m)). If you'd need to sort them before that, you'd get O(n*log n+m*log m).
  • Best would be using a hash table for O(1) lookup, resulting in O(n+m). You can easily use a JS object for that:
var counts = {};
for (var i=0; i<array_new.length; i++)
    counts[array_new[i].id] = (counts[array_new[i].id] || 0) + 1;

return array_old.every(function(item) {
    return item.idin counts && counts[item.id]--;
});

(Demo)

Solution 4:

Throwing in a wild alternative just because it works and it's ultimately easier to read (no idea about performance, but let's skip that since I'm doing it to show another way to do it)

var ids_old = $.map(array_old, function(item) { return item.id; });
var ids_new = $.map(array_new, function(item) { return item.id; });

var duplicates = $.grep(ids_old, function(i, id) { 
    return $.inArray(id, ids_new) !== -1;
});

Be aware that the end result is that you get a list of the duplicate IDs themselves, this other alternative lets you collect the item itself:

var ids_new = $.map(array_new, function(item) { return item.id; });

var duplicates = $.grep(array_old, function(i, item) { 
    return $.inArray(item.id, ids_new) !== -1;
});

Bonus points: even if his example is pure jQuery, notice that on ECMAScript5 compliant browsers you can use the native array counterparts to achieve the same result.

Post a Comment for "Compare Two Multidimensional Arrays In Javascript"