Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
How to compare 2 arrays of objects with duplicate elements

I want to compare 2 array of objects with floating point property to see if they are equal, regardless of the order of elements. The array may contain duplicate elements. I already have a function to determine whether two objects are equal, however, I have no idea how to compare 2 arrays.

const arr1 = [
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
const arr2 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.2
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];

const compareObject = (o1, o2) => {
  if (o1.foo !== o2.foo) {
    return false;
  }
  const threshold = 0.00000001;
  return Math.abs(o1.bar - o2.bar) < threshold;
};
const compareArr = (arr1, arr2) => {
  // how to compare?
};

compareArr(arr1, arr2) // should return true

Clarify: The function should only return true if 2 arrays are exactly matched regardless of the order of elements, and there may be duplicate elements inside an array, i.e.

const arr1 = [obj1, obj1, obj2];
const arr2 = [obj1, obj2, obj2];

compareArr(arr1, arr2) // should return false
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const arr1 = [
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
const arr2 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.2
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];

const arr3 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.1
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
function sortObject( a, b ) {
  if ( a.foo < b.foo ){
    return -1;
  }
  if ( a.foo > b.foo ){
    return 1;
  }
  
  if ( a.bar < b.bar ){
    return -1;
  }
  if ( a.bar > b.bar ){
    return 1;
  }
}

const compareObject = (o1, o2) => {
  if (o1.foo !== o2.foo) {
    return false;
  }
  const threshold = 0.00000001;
  return Math.abs(o1.bar - o2.bar) < threshold;
};

arr1.sort(sortObject);
arr2.sort(sortObject);
arr3.sort(sortObject);

function compare (arr1, arr2) {
  let result = true;
  if (arr1.length != arr2.length) return false;
  arr1.every(function(element, index) {
    // Do your thing, then:
    if (!compareObject(element, arr2[index])) {
      result = false;
      return false;
    }
    return true;
  });
  return result;
}

console.log(compare(arr1, arr2));
console.log(compare(arr1, arr3));

about 4 years ago · Juan Pablo Isaza Report

0

You can use the every function

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Example :

const arr1 = [
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
const arr2 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.2
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];

const compareObject = (o1, o2) => {
  if (o1.foo !== o2.foo) {
    return false;
  }
  const threshold = 0.00000001;
  return Math.abs(o1.bar - o2.bar) < threshold;
};
const compareArr = (arr1, arr2) => {
  return arr1.every(arr1Item => arr2.find(arr2Item => compareObject(arr1Item, arr2Item)))
};

console.log(compareArr(arr1, arr2)) // should return true

You can also use the some function

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

And check if one element does not pass the test.

const arr1 = [
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];
const arr2 = [
  {
    foo: "obj2",
    bar: 0.1 + 0.2
  },
  {
    foo: "obj1",
    bar: 0.1
  },
  {
    foo: "obj2",
    bar: 0.3
  }
];

const compareObject = (o1, o2) => {
  if (o1.foo !== o2.foo) {
    return false;
  }
  const threshold = 0.00000001;
  return Math.abs(o1.bar - o2.bar) < threshold;
};
const compareArr = (arr1, arr2) => {
  return !arr1.some(arr1Item => !arr2.find(arr2Item => compareObject(arr1Item, arr2Item)))
};

console.log(compareArr(arr1, arr2)) // should return true

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!