get nested array lengths in handlebars?

- 1 answer

Ad

I have the following array layout :

var indicators = 
{
    items:
    {
        array1:[],
        array2:[]
    }       
}; 

and I have the following handlebars code to make a template :

var source =
    '{{#items}}' +
        '<div class="listItem" onclick="clickListItem({{@index}});">' +
        '<div class="leftSide">' +
        '<div class="listTitle"> {{SOMETHING MUST GO HERE, RIGHT?}} </div>' + 
        '</div>' +
        '</div>' +
        '{{/items}}';

var template = Handlebars.compile(source);

$('.list').html(template(indicators));

As you can probably see here - I want to create a list that will appear on screen, which simply displays the length of each array inside indicators (array1, and array2) - How do I get that data for each array - The code I have does iterate over indicators.items, but how do I get the length of them?

Ad

Answer

Ad

Have you tried this?. to get the data for each array inside your items object

{{#items}}
   {{#array1}}
   {{/array1}}

   {{#array2}}
   {{/array2}}
{{/items}}

can you try this?.. it gets the array lengths inside your items object

var arrayLengths = [];
var objects = { 
  array1: [1,2,3,4,5,12,55],
  array2: [1,2,3,4,5,6,7,8],
  array3: [1,2,3,4,5,3,4],
  array4: [1,2,3,4,5,10,23],
  array5: [1,2,3,4,5,440,120]
};
for(var key in objects) {
    var value = objects[key].length;
    arrayLengths.push(value);
}
console.log(arrayLength);
Ad
source: stackoverflow.com
Ad