数组concat返回空数组 [英] Array concat returns empty array
本文介绍了数组concat返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要在事件处理程序中使用 concat
将多个项目添加到数组中,如下所示:
I'm adding several items to an array using concat
in an event-handler as follows:
var selectedValues = [];
$.each($('#selected-levels').data("kendoListBox").dataSource.data(), function(i, item) {
selectedValues.concat([ item.id ])
});
return {
"selected" : selectedValues
};
这总是返回 {level-selected:Array(0)}
即使我检查了 dataSource
中是否有一些 item
(通过调试器逐步完成)
this always returns {level-selected: Array(0)}
even though I have checked that there are some item
s in the dataSource
(by stepping through with debugger)
为什么项目不出现在数组中?
why don't the items appear in the array?
推荐答案
concat
不改变数组,您需要将值设置回selectedValues
selectedValues = selectedValues.concat([ item.id ])
或使用 push
selectedValues.push( item.id )
这篇关于数组concat返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文