繁体   English   中英

如何将.each调用的返回值保存到数组中

[英]how do i save return values from a .each call into an array

我有一个函数调用:

CreateNode(e,control);// which will return an ID.  
                      // e i leave alone, but i was thinking that i 
                      // could pass the object into the function this way optionally.


function CreateNode(e, control){
if(!control) control = this;
// for rest of function, calls to the object are $(control) instead of $(this).
//...
}

然后,我有一个要迭代的选择器:

$(control_group).each(createNode);

有没有办法从中构建IDS列表,例如:

var arr = [];
arr.push($(control_group).each(createNode));

我正在做一个递归控件构建器,它在控件中制作控件,因此我想将标识符返回到child属性。 那就是我要和arr做的事。

我的一个主意是做一些简单的事情,例如:

var arr = [];
$(control_group).each(function(e){
    arr.push(createNode(e,$(this));
});

这正是.map()作用:

var arr = $(control_group).map(createNode).get();

.map()返回一个jQuery对象; 如果要使用普通数组,则需要对其进行.get()处理。

暂无
暂无

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

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