我如何使用这种模式存储数据 - 对象里面的对象里面的对象或对象里面的对象

时间:2021-12-01 08:14:08

Please give me answer use JavaScript code. How i can store data like this

请给我回答使用JavaScript代码。我如何存储这样的数据

let obj = new Object;
obj.prop = 5; // Good
// but when i put third value like obj.prop.newObjectInside= 5 /Return error/
// How i can do it? or for example 
obj.prop.array = 5 // Return error

I need add third object or array inside and use this pattern in my loop.

我需要在里面添加第三个对象或数组,并在我的循环中使用这个模式。

Thank you and if you know some articles or reference please write me too.

谢谢,如果你知道一些文章或参考,请写信给我。

1 个解决方案

#1


0  

Try

obj.prop = { array : 5 } 

or since it seems like you desire it to be an array you can do

或者因为你似乎希望它成为一个阵列,你可以做到

obj.prop = { array : [5] } 

You should then be able to do array ops like so

那么你应该能够像这样做数组操作

obj.prop.array.push(value);

Note the key here is to ensure that the second child is initialized, so you have to at least have an empty array

注意,这里的关键是确保初始化第二个子节点,因此您必须至少有一个空数组

obj.prop = { array : [] }

#1


0  

Try

obj.prop = { array : 5 } 

or since it seems like you desire it to be an array you can do

或者因为你似乎希望它成为一个阵列,你可以做到

obj.prop = { array : [5] } 

You should then be able to do array ops like so

那么你应该能够像这样做数组操作

obj.prop.array.push(value);

Note the key here is to ensure that the second child is initialized, so you have to at least have an empty array

注意,这里的关键是确保初始化第二个子节点,因此您必须至少有一个空数组

obj.prop = { array : [] }