Answers for "javascript array compare elements"

2

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
Posted by: Guest on August-05-2021
6

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
Posted by: Guest on October-07-2020
0

how to compare elements in an array

for (let i = 0; i < a.length; i++) {
    for (let k = i + 1; k < a.length; k++) {
        if (a[i] != a[k]) {
            //do stuff
        }
    }
}
Posted by: Guest on November-03-2020
0

array compare detailed

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
Posted by: Guest on August-16-2021

Code answers related to "javascript array compare elements"

Code answers related to "Javascript"

Browse Popular Code Answers by Language