抱歉我的英语不太好。
我正在使用JavaScript编程,我已经创建了一个数组,现在想要向其中添加数值。
var attribute_sets = [];
$('.attribute_set :selected').each(function(i, selected){
attribute_sets[i]['id'] = $(selected).val(); // getting id
attribute_sets[i]['name'] = $(selected).text(); // getting name
});
出现了错误
类型错误:attribute_sets[i]未定义
也尝试了这个方法
attribute_sets[i]['id'].push($wk_jq(selected).val());
仍然出现相同的错误
请问有人可以指导我如何在这个JS数组中插入值吗? 我希望得到以下输出结果
array
[0]
'id':'1',
'name':'abc'
[1]
'id':'2',
'name':'xyz'