2013-09-24 33 views
0

我有一個功能我叫:我如何從一個。每次調用保存返回值到一個數組

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)); 

我做一個recurive控件生成,這使得管制在控制和所以我想將標識符返回到一個子屬性。那就是我要做的arr

我的一個想法是做這樣簡單的東西:

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

回答

4

這正是.map()做:

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

.map()返回一個jQuery對象;如果你想要一個普通的數組,你需要.get()它。

+0

我的情況似乎從.map()返回一個大小爲1的數組,最後一次執行createNode。還有什麼其他事情我不知道/ – Fallenreaper

+0

什麼是$(control_group)?請注意'map()'可能不會傳遞你想要的參數。 – SLaks

相關問題