Posted inJavaScript / jQuery

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

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype