简体   繁体   English

在javascript中将数据添加到对象数组

[英]Add data to object array in javascript

I know I can do something like that: 我知道我可以这样做:

var data = [
    {
        name: 'test1',
        value: 1
    }
];

But I want to fill data with dynamic values (from HTML). 但我想用动态值(来自HTML)填充data So I will have var data = []; 所以我将有var data = []; and how can I fill this one? 我怎么能填这个呢? I know, it must be somewhere on the internet, but I don not know the "right words" to search. 我知道,它必须在互联网上的某个地方,但我不知道要搜索的“正确的词”。

Note: data have to be array filled with objects. 注意: data必须用对象填充数组。

You can use the 'push' method. 您可以使用'推送'方法。

 var a = []; a.push({item: 'a', value : 'a'}); console.log(a); 

You can use .push method, like so 您可以使用.push方法,就像这样

 var data = []; data.push({ name: 'test1', value: 1}); data.push({ name: 'test2', value: 2}); data.push({ name: 'test3', value: 3}); console.log(data); 

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

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