There is a limitation when using Objects inside the array. IndexOf, Contains or Remove don’t work fine with objects as value.
Here an example in plain JavaScript with indexOf:
const array1 = [];
const array2 = [];
array1.push(1);
array2.push({n:1});
console.log(array1.indexOf(1)); // returns 0
console.log(array2.indexOf({n:1}); // returns -1
Array1 it looks up the value 1
and finds it at index 0
. The second array it looks for the object {n:1}
but can’t find it, this is because the objects are not the same. The only way to check in an array if there is an object where n
is 1
is by looping through it and check for each object the property n
. It is a bit difficult to explain to someone without a lot of programming experience, so hope it makes some sense.
If you only use it as an lookup then store the value as a string or number instead of an object. We can change the behavior for these actions to do a deep compare of objects to match them, but that will have some impact on performance. We wanted to keep the array lightweight and fast.