我定义了这个:
var data ={};
我希望对象中的每个“行”都能得到这样的东西:
{field1: "bananas", field2:'test', field3:111, field4:"23.4", field5:"bob"},
{field1: "fruit", field2:'test again', field3:222, field4:"30", field5:"john"}
我知道我可以这样做来添加动态和静态数据:
data['field1']= docType;
data['field2'] = docRef;
data['field3'] = "test3";
data['field4'] = mydynamicdata;
data['field5'] = "test5";
但是如何添加不同的项目行?这段代码只会添加一个项目集,对吗?我该如何添加第二个?
谢谢你!答案 0 :(得分:2)
您需要一个对象数组 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
var data = [];
data.push({field1: "bananas", field2:'test', field3:111, field4:"23.4", field5:"bob"})
data.push({field1: "fruit", field2:'test again', field3:222, field4:"30", field5:"john"})
然后你可以做这样的改变:
data[0]['field1'] = 'new value'
//or:
data[0].field1 = 'new value'
其中0
是列表中项目的索引。所以data[1]
将是第二个对象,依此类推......