Answers for "compare two arrays"

3

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
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
1

comparing 2 array

//javascript by the davido skills
const arr1 = [1, 2, 3];
const arr2 = [1, 3, 3];

if (arr1.length !== arr2.length) return console.log("false");
let j = 0
for (let i = 0; i < arr1.length; i++) {
        if (arr1[i] === arr2[j]) {
            console.log("yes match", arr1[i], arr2[j]);
        }
        else{
          console.log("no match", arr1[i], arr2[j]);
        }
    j++;
}
//javascript by the davido skills
Posted by: Guest on May-27-2021
0

compare two arrays

echo ${Array1[@]} ${Array2[@]} | tr ' ' '\n' | sort | uniq -u
Posted by: Guest on May-31-2021

Code answers related to "compare two arrays"

Browse Popular Code Answers by Language