jQuery的返回所有阵列 [英] jQuery return all Array
本文介绍了jQuery的返回所有阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能获得的功能外阵列中的所有内容?
$。每个(Basepath.Templates,功能(我){
templateArray =新的Array({标题:Basepath.Templates [I] .Template.name,SRC:视图/+ Basepath.Templates [I] .Template.id,说明:Basepath.Templates [I] .Template.name} );
});的console.log(templateArray); //仅包含一个阵列;
编辑:
这是输出,我想
templateArray [
{
标题:一,
SRC:视图/ 1
介绍:Descrption 1
},
{
标题:两节,
SRC:视图/ 2,
介绍:Descrption 2
},
{
标题:一,
SRC:视图/ 3
介绍:Descrption 3
}
]
解决方案
您正在覆盖 templateArray
在每个迭代。尝试 .MAP()
代替每()
:
VAR templateArray = $ .MAP(Basepath.Templates,函数(TPL,I){
返回{
标题:tpl.Template.name,
SRC:视图/+ tpl.Template.id,
说明:tpl.Template.name
};
});
的console.log(templateArray); //现在只有一个阵列,含有模板对象;
How can i get all the contents of the array outside the function?
$.each(Basepath.Templates, function(i){
templateArray = new Array({title: Basepath.Templates[i].Template.name, src: 'view/'+Basepath.Templates[i].Template.id, description: Basepath.Templates[i].Template.name});
});
console.log(templateArray); //contains only one array;
Edit:
this is the output i want
templateArray[
{
title: "one",
src: "view/1",
description: "Descrption 1"
},
{
title: "two",
src: "view/2",
description: "Descrption 2"
},
{
title: "one",
src: "view/3",
description: "Descrption 3"
}
]
解决方案
You're overwriting templateArray
in each iteration. Try .map()
instead of each()
:
var templateArray = $.map(Basepath.Templates, function(tpl, i){
return {
title: tpl.Template.name,
src: 'view/'+tpl.Template.id,
description: tpl.Template.name
};
});
console.log(templateArray); // now is only one array, containing template objects;
这篇关于jQuery的返回所有阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文