Answers for "js compare 2 arrays"

15

javascript difference between two arrays

let difference = arr1.filter(x => !arr2.includes(x));
Posted by: Guest on April-03-2020
4

javascript compare two arrays of objects get same elements

var result = result1.filter(function (o1) {
    return result2.some(function (o2) {
        return o1.id === o2.id; // return the ones with equal id
   });
});
// if you want to be more clever...
let result = result1.filter(o1 => result2.some(o2 => o1.id === o2.id));
Posted by: Guest on August-03-2020
2

es6 compare two arrays

let difference = arrA.filter(x => !arrB.includes(x));
Posted by: Guest on September-24-2020
2

diff two arrays javascript

function diffArray(arr1, arr2) {
  return arr1
    .concat(arr2)
    .filter(item => !arr1.includes(item) || !arr2.includes(item));
}
Posted by: Guest on August-13-2020
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
2

javascript Compare two arrays regardless of order

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

// Examples
isEqual([1, 2, 3], [1, 2, 3]);      // true
isEqual([1, 2, 3], [1, '2', 3]);    // false
Posted by: Guest on July-02-2020

Code answers related to "js compare 2 arrays"

Code answers related to "Javascript"

Browse Popular Code Answers by Language