简体   繁体   English

向数组Javascript添加元素

[英]Adding element to array Javascript

I have a array that looks like the following: 我有一个看起来像下面的数组:

在此处输入图片说明

I would like to add a new item, named "attributes", which is also an array. 我想添加一个新项目,名为“ attributes”,它也是一个数组。 I can I do that? 我能做到吗?

Thanks. 谢谢。

 var test = [{ enabled: true, key: 'FIN28', text: 'Service Agreement' }, { enabled: true, key: 'test', text: 'test test' }]; for (var i = 0; i < test.length; i++) { test[i]['attributes'] = []; } console.log(test); 

According to your comment, you want to add an attribute named "attribute" to the object inside your array. 根据您的评论,您想向数组中的对象添加一个名为“ attribute”的属性。 you can do it like this : 你可以这样做:

myarray[0].attribute = myvalue;

If you want to do it for every item of your array, you can do this : 如果要对数组的每个项目执行此操作,则可以执行以下操作:

for(var idx in myarray)
    myarray[idx].attribute = myvalue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM