如何在关联数组javascript中填充值
对不起,我英语不好 我正在使用javascript,我已经构建了一个数组,我想在这个数组中输入值如何在关联数组javascript中填充值,javascript,arrays,associative-array,Javascript,Arrays,Associative Array,对不起,我英语不好 我正在使用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(); //
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
});
这给了我错误
TypeError:属性_集[i]未定义
我也试过这个
attribute_sets[i]['id'].push($wk_jq(selected).val());
还是有同样的错误吗
有人能告诉我如何在这个JS数组中插入值吗。
我想要这样的输出
array
[0]
'id':'1',
'name':'abc'
[1]
'id':'2',
'name':'xyz'
使用函数
attribute_sets = $('.attribute_set :selected').map(function(i, selected){
return {
'id' : $(this).val(),
'name' : $(this).text(),
}
}).get();
试一试
你想得到什么类型的元素?我正在尝试从multi-select中识别和命名,并将此数组发送给phpThank。谢谢,你工作起来很有魅力……:)非常感谢你这么多的工作就像一个魔咒…:)非常感谢
$('.attribute_set :selected').each(function(i, selected){
attribute_sets.push({
id: $(selected).val(), // getting id
name: $(selected).text() // getting name
});