Array.IndexOf()
function behaves different for strings and objects.
stringBehaviour();
objectBehaviour();
function stringBehaviour() {
let s1 = "helmet";
let s2 = "trousers";
let collection = [];
collection.push(s1);
collection.push(s2);
console.info(collection.indexOf("trousers"));
};
function objectBehaviour() {
let obj1 = {item: "helmet"};
let obj2 = {item: "trousers"};
let collection = [];
collection.push(obj1);
collection.push(obj2);
console.info(collection.indexOf({item: "trousers"}));
};
what can i do to have indexOf()
not looking for the same instance but for equality of the objects elements so that the function objectBehaviour()
would return 1?