Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js compare lists

// compare two arrays where order might differ
const array1 = [1,2,3,4,5];
const array2 = [3,4,1,2,5];

const compareArrays = (array1, array2) => {
  return (
    array1.length === array2.length && 
    array1.every((el) => array2.includes(el));
  );
};

compareArrays(array1, array2); // true
Comment

js compare arrays

var a1 = [1,2,3];
var a2 = [1,2,3];
console.log(a1==a2);    // Returns false
console.log(JSON.stringify(a1)==JSON.stringify(a2));    // Returns true
Comment

javascript compare arrays

Array.prototype.equals = function(arr2) {
  return (
    this.length === arr2.length &&
    this.every((value, index) => value === arr2[index])
  );
};

[1, 2, 3].equals([1, 2, 3]);	// true
[1, 2, 3].equals([3, 6, 4, 2]);	// false
Comment

how to compare arrays in js

// THE PROBLEM:
const firstArray = ["cookies", "milk", "chocolate"]
const secondArray = ["cookies", "milk", "chocolate"]
console.log(firstArray == secondArray) // always returns FALSE

// THE SOLUTION:
if(JSON.stringify(firstArray) === JSON.stringify(secondArray)){
  console.log("firstArray is the same as the secondArray")
} else {
  console.log("firstArray is different from the secondArray")
}
Comment

How to compare arrays in JavaScript?

// Warn if overriding existing method
if(Array.prototype.equals)
    console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
    // if the other array is a falsy value, return
    if (!array)
        return false;

    // compare lengths - can save a lot of time 
    if (this.length != array.length)
        return false;

    for (var i = 0, l=this.length; i < l; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && array[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].equals(array[i]))
                return false;       
        }           
        else if (this[i] != array[i]) { 
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;   
        }           
    }       
    return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});
Comment

Comparing two lists in Javascript

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

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

equals(a, b); // true
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript get data attribute of selected option 
Javascript :: Function in JavaScript that can be called only once 
Javascript :: jquery selected label option 
Javascript :: regex to extract a phone number with country code 
Javascript :: jquery on change function not working 
Javascript :: jquery trigger 
Javascript :: sleep javascript 
Javascript :: jquery select option value id no not exists 
Javascript :: colors.xml" already exists! 
Javascript :: ascii to hex js 
Javascript :: framer motion styled components 
Javascript :: react native image disable fade in onload 
Javascript :: convert hex to decimal javascript 
Javascript :: indexof case insensitive javascript 
Javascript :: angular copy 
Javascript :: jq html remove disabled 
Javascript :: combinantion of single array in node js 
Javascript :: nextjs absolute import 
Javascript :: how to make sure footer is fixed at bottom of page 
Javascript :: discord.js bot activity 
Javascript :: react native get route name 
Javascript :: mathjs get element from matrix 
Javascript :: json_encode escape 
Javascript :: trigger ctrl + p or print page with javascript 
Javascript :: get value of element html js 
Javascript :: javascript push in specific index 
Javascript :: react scroll to top 
Javascript :: javascript is variable a string 
Javascript :: javascript regex url 
Javascript :: jquery calculate datetime difference 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =