如果我们使用new Array(len).fil({})创建一个对象数组;然后在任何对象中添加一个键,它会反映在所有3个对象中 [英] If we create an array of objects using new Array(len).fil({}); and then add a key in any of the objects, it gets reflected in all the 3 objects

查看:224
本文介绍了如果我们使用new Array(len).fil({})创建一个对象数组;然后在任何对象中添加一个键,它会反映在所有3个对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们使用new Array(3).fill({})创建一个对象数组;然后在任何对象中添加任意键,它会反映在所有3个对象中。

If we create an array of objects using new Array(3).fill({}); and then add any key in any of the objects, it gets reflected in all the 3 objects.

我创建了一个对象数组,如:
const arr = new Array(3).fill({});

I made an array of objects like: const arr = new Array(3).fill({});

然后我尝试做arr [0] [key] =value

And then I tried doing arr[0][key] = "value"

这更新了数组中所有3个对象的键。

This updated the key in all 3 objects in the array.

推荐答案

你放了同一个对象在3个不同的地方。它仍然是相同的。

You put the same object at 3 different places. It's still the same.

如果您想要3个不同的对象,您可以这样做:

If you want 3 different objects, you may do this for example:

const arr = new Array(3).fill().map(()=>({}));

这篇关于如果我们使用new Array(len).fil({})创建一个对象数组;然后在任何对象中添加一个键,它会反映在所有3个对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆