gpt4 book ai didi

JavaScript : how to add a new element to the array that has key value pairs?

转载 作者:行者123 更新时间:2023-12-01 01:54:34 25 4
gpt4 key购买 nike

我有一个数组a[0],它已经有自己的键值对。现在我想将另一个元素添加到数组 a 中,即 a[1] 并向其添加一个属性。

我想将键值对添加到 a[1] 中新添加的属性。

例如:a[1] 现在已经有了地址。 现在我想添加键值对,例如

"street" : "Avenue St"
"pin" : "560064"

然后我想添加另一个键值对,例如“city”:“Tokyo”

最后它必须看起来像:

address : {
"street" : "Avenue St"
"pin" : "560064"
}
city : "Tokyo"

这应该适用于相同的数组索引:a[1]。

我该怎么做?

最佳答案

您可以通过以下方式进行:

var a = [{name: "John"},{address: "Old Avenue St"}];
a[1].address = {"street" : "Avenue St", "pin" : "560064"};

console.log(a);

关于JavaScript : how to add a new element to the array that has key value pairs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51147055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com