JavaScript : Compare two arrays

Conditions :

  • You have a arrays by structure with following :

array1 : [

{“Name” : “John”, “Age”: “24”},

{“Name”: “Kata”, “Age”: “25}

]

  • You have another arrays by structure with following:

array1 : [

{“Name”: “Kata”, “Age”: “25}

{“Name” : “John”, “Age”: “24”},

]

Expected : You want to compare equals 2 this arrays

Solution : You should have a flag to check equals or not.

var checkEquals = function compareEquals(array1, array2){

var flag = false;

for(var i = 0; i < array1.length; i++){

for(var j = 0; j < array2.length; j++){

if (array1[j].Name.toUpperCase() === array2[i].Name.toUpperCase()) {
flag = true; break;
} else {  flag = false;}}

if (flag === false) return true;}

return false;

}

Published by Nguyen Quoc Dung

"If you only do what you can do You never be more than you are now"

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: