compare two arrays javascript – How to compare arrays in JavaScript?

compare two arrays javascript To compare arrays, loop through them and compare every value: Arrays are objects in JavaScript, so the triple equals operator === only returns true if the arrays are the same reference.

compare two arrays javascript

Find the difference between two arrays in JavaScript Using Array.prototype.filter() function, Using jQuery, Using Underscore/Lodash Library,

1. Using Array.prototype.filter() function

indexOf() method

var website_str = [ 1, 2, 3, 4, 5 ];
var old_website = [ 4, 5, 6 ];
 
var results = website_str.filter(x => old_website.indexOf(x) === -1);
console.log(results);
 

Equality comparison

const a = [1, 2, 3];
const b = [1, 2, 3];

a === b; // false

using JSON.stringify

const equals = (a, b) => JSON.stringify(a) === JSON.stringify(b);

const a = [1, 2, 3];
const b = [1, 2, 3];

equals(a, b); // true

Using jQuery

Example 1

const { JSDOM } = require("jsdom");
const { window } = new JSDOM();
var $ = require("jquery")(window);
 
var website_str = [ 1, 2, 3, 4, 5 ];
var old_website = [ 4, 5, 6 ];
 
var results = $(website_str).not(old_website).get();
console.log(results);
 

Example 2
const { JSDOM } = require("jsdom");
const { window } = new JSDOM();
var $ = require("jquery")(window);
 
var website_str = [ 1, 2, 3, 4, 5 ];
var old_website = [ 4, 5, 6 ];
 
var results = $.grep(website_str, (item) => $.inArray(item, old_website) === -1);
console.log(results);
 

JavaScript Program to Compare Elements of Two Arrays

program to compare two arrays: Compare Arrays Using JSON.stringify()

function diffcomparedata(love_point, trust_points) {

    const result = JSON.stringify(love_point) == JSON.stringify(trust_points)

    if(result) {
        console.log('The arrays have the same elements.');
    }
    else {
        console.log('The arrays have different elements.');
    }

}

const love_point = [1, 3, 5, 8];
const trust_points = [1, 3, 5, 8];

diffcomparedata(love_point, trust_points);

Compare Arrays using for Loop
program to extract value as an array from an array of objects

function diffcomparedata(love_point, trust_points) {

    if(love_point.length != trust_points.length) {
        return false;
    } 
    else { 
        let result = false;
        
        for(let i=0; i<love_point.length; i++) {

            if(love_point[i] != trust_points[i]) {
                return false;
            }
            else {
                result = true;
            }

        }
        return result;
        
  }

}

const love_point = [1, 3, 5, 8];
const trust_points = [1, 3, 5, 8];

const result = diffcomparedata(love_point, trust_points);

if(result) {
    console.log('The arrays have the same elements.');
}
else {
    console.log('The arrays have different elements.');
}

Using Underscore/Lodash Library

Example

var _ = require('underscore');
 
var love_point = [ 1, 2, 3, 4, 5 ];
var trust_points = [ 4, 5, 6 ];
 
var results = _.difference(love_point, trust_points);
console.log(results);

Don’t Miss : PHP Compare Two Arrays Values

I hope you get an idea about compare two arrays javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.