get nested array lengths in handlebars?
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
Related Questions
Ad
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad