0

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
AARon
  • 73
  • 1
  • 8

2 Answers2

1

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));
Xupitan
  • 1,601
  • 1
  • 8
  • 12
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
RenaudC5
  • 3,553
  • 1
  • 11
  • 29