Below I have defined an object and then created a 3x3 array with each element holding an instance of myObject. I'm expecting function removeNumberFromArray to iterate over each element and therefore each object and remove the number 32 from the array called myNumbers. My comments indicate what it actually logs to the console. The result is an empty array and I was expecting to have each object within the 9x9 array to have myNumbers array equal to [7,51,2,0,9].
I can't see where I am going wrong and I have tried breaking the problem down into smaller steps but splice seems to work, any ideas?
I'm using VS Code with Liveserver Extension with Firefox Browser.
const myObject = {
myNumbers: [7,32,51,2,0,9],
myName: "John",
myLastName: undefined,
age: 26
};
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
myArray[i][j] = Object.create(myObject);
}
}
function removeNumberFromArray(numberToRemove) {
myArray.forEach(ele => {
ele.forEach(square => {
let index = square.myNumbers.indexOf(numberToRemove);
square.myNumbers.splice(index, 1);
})
});
}
console.log(myArray[0][0].myNumbers); //[7, 32, 51, 2, 0, 9]
removeNumberFromArray(32);
console.log(myArray[0][0].myNumbers); //[]