简体   繁体   中英

Compare two arrays of objects

I need to compare two arrays. If I test if Object1==Object2 I am testing if the objects point to the same address in memory. I actually want to compare them based on their properties values. Let me show you what I mean.

I have a class in javascript:

function MyClass(var1,var2,var3)
{
   this.var1 = var1;
   this.var2 = var2;
   this.var3 = var3;
}

Now I will be able to create a new object of that class as:

var myNewObject = new MyClass(1,"hello",3);

and I will be able to access the properties of that object as:

myNewObject.var2 = "New value";

I have two arrays:

 var array1 = new Array();
 var array2 = new Array();

both arrays are populated with MyClass objects. In other words if array1 has 3 elements I know that they will be of type MyClass meaning I could do something like: array1[2].var1 = foo

* I will like to compare array1 with array2 based on the values of their properties *

for example if

   array1 = [ [1,"hello",4] , [0,"foo",5] ]; 

   array2 = [ [0,"foo",5555] [1111,"hello",4]];

   note: // [1,"hello",4] means a MyClass object with var1=1, var2="hello", var3 = 4

I will like to have a method that will tell me the changes. In this example var1 changed to 1111 and var3 changed from 5 to 5555.

I will probably be able to do it if I spend a lot of ours doing it. I am not to good at algorithms and maybe there is a simple way of doing it.


edit

I will like to know the changes that I will have to make to array1 in order to make it equal to array2.

Using literal notation instead of pesudo code:

> array1 = [ {var1:1, var2:"hello", var3:4},
>            {var1:0, var2:"foo", var3:5}
> ];
> array2 = [ {var1:0, var2:"foo", var3:5555},
>            {var1:1111, var2:"hello", var3:4}
> ]; 
>
> I will like to have a method that will tell me the changes. In
> this example var1 changed to 1111 and var3 changed from 5 to 5555.

How do you know to compare the first member of the first array with the second member in the second array? Similarly for array1[1] vs array2[0]?

The objects do not seem to have unique identifiers or a particular order in their container arrays, so how do you know which ones to compare to determine differences or changes required?

"Making it equal" sounds like array1 = array2.slice(0) , but that seems not to be what you want. So try the following:

for (var i=0, l=Math.max(array1.length, array2.length); i<l; i++) {
    if (! array1[i]) {
        alert("You need to make a copy of "+array2[i]);
        continue;
    }
    if (! array2[i]) {
        alert("You need to delete "+array1[i]);
        continue;
    }
    for (var j in array2[i]) {
        if (array2.hasOwnProperty(i)) // needed only when MyClass has a prototype with enumerable properties
            if (array1[i][j] !== array2[i][j])
                alert("You need to change "+j+" of "+array1[i]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM