Issue
The code loops 2 times. The first loop should add {}
to the empty testArr
array and the second loop should change the item at testArr[0]
to say "new object"
. After each loop, the length of the array should not
change.
Question
How do I get it to stop adding new objects to an array if an object already exists? Emtpy object or not, it still does the same thing? Is it just that Js treats objects a certain way?
I’m specifically trying to work with objects, but if this doesnt work is there a simple solution?
Code
testArr = [];
function test_arr_includes() {
if (testArr.includes({}) === false) {
testArr.push({})
} else {
testArr[0] = "new object"
}
}
for (let i = 1; i <= 2; i++) {
// update({ id: "cabbage" });
test_arr_includes();
console.log(testArr)
}