Rachel
Rachel

Reputation: 1159

Comparing two arrays using javascript

var array1 = [column1,column2,column3,column4];
var array2 = [column1 [empid],column2 [mobno],column4 [place]];

is above case possible?

Upvotes: 0

Views: 71

Answers (2)

harsh
harsh

Reputation: 1551

if you dont want to use any library then you can do

for(elem in array2){

    var index = array1.indexOf(array2[elem]);
    if(index > -1)
    array1.splice(index,1);

}

array2 = array1.concat(array2).sort();

Upvotes: 0

Cubed Eye
Cubed Eye

Reputation: 5631

I would recommend using a library like Underscore.js, it has functions like _.union() that would do what you described. If you don't want to the library you could have a look at their implementation.

Using your example:

array2 = _.union(array1, array2);

Upvotes: 1

Related Questions