2 回答
TA贡献1836条经验 获得超3个赞
通过使用,Object.create(myObject)
您正在使用原型继承来创建一个继承自 的对象myObject
,这不是您想要的。我建议您创建一个小型“工厂”函数来生成像这样的独特对象:
TA贡献1863条经验 获得超2个赞
const arrayLike = { length: 3 };
const myArray = Array.from(arrayLike, () => Array.from(arrayLike));
const objectFactory = () => ({
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] = objectFactory();
}
}
function removeNumberFromArray(numberToRemove) {
myArray.forEach(ele => {
ele.forEach(square => {
let index = square.myNumbers.indexOf(numberToRemove);
if(~index) 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); //[]
添加回答
举报