作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数组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/
我是一名优秀的程序员,十分优秀!