簡體   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