Skip to content Skip to sidebar Skip to footer

JQuery Return All Array

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.nam

Solution 1:

Try setting up the variable for the array before the .each() call and add to the array on each iteration.

var templateArray = [];
$.each(Basepath.Templates, function(i){
    templateArray.push({title: Basepath.Templates[i].Template.name, src: 'view/'+Basepath.Templates[i].Template.id, description: Basepath.Templates[i].Template.name});
});

console.log(templateArray); //should work

Solution 2:

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;

Post a Comment for "JQuery Return All Array"