I currently pass an array of objects to a handlebars helper function to sort and return a new array. I want to use the new array to fill in my html.
My handlebars template is as follows:
FoodGroup: [
{
name: "vegetables",
foods: [
{
name: "celery",
description: "lorem ipsum..."
},
{
name: "cucumber",
description: "lorem ipsum..."
}
]
},
{
name: "fruits",
foods: [
{
name: "banana",
description: "lorem ipsum..."
},
{
name: "apple",
description: "lorem ipsum..."
}
]
}
]
This is what my helper function looks like:
<script type="text/javascript">
Handlebars.registerHelper("sortFood", function(items, options){
var allFood = [];
// iterate through each FoodGroup
for (var i = 0; i < items.length; i++) {
var seperatedArray = items[i].foods;
// iterate through all foods within each group and push into allFood array
for (var j = 0; j < seperatedArray.length; j++) {
allFood.push(seperatedArray[j]);
}
}
// sort all foods alphabetically
allFood.sort(function(a,b){
var textA = a.name.toLowerCase();
var textB = b.name.toLowerCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
// create new object for individual arrays for first letter of each food name
var sortedFood = {};
for (var i=0; i<allFood.length; i++) {
var l = allFood[i].name.substring(0,1);
// ensure object has key
if (!sortedFood[l]) sortedFood[l] = [];
sortedFood[l].push(allFood[i]);
}
console.log(sortedFood)
return sortedFood;
});
</script>
Currently I pass an array to the helper function like this
{{#sortFood FoodGroup}}
{{/sortFood}}
And the helper function returns a sorted object that looks like this. The helper takes the original object FoodGroup and pulls out all of the food objects and sorts it alphabetically in an array alphabetically:
sortedFood= {
A: [
{
name: "Apple",
description: "lorem ipsum ...."
}
],
B: [
{
name: "Banana",
description: "lorem ipsum ...."
}
],
C: [
{
title: "Celery",
summary: "lorem ipsum ...."
},
{
title: "Cucumber",
summary: "lorem ipsum ...."
},
{
title: "Customer Surveys",
summary: "lorem ipsum ...."
}
]
.. etc
}
I want to generate a list in my html that looks like this using the newly generated "sortedFoods" array from the handlebars helper
<h1>A</h1>
<li>Apple</li>
<h1>B</h1>
<li>Banana</li>
<h1>C</h1>
<li>Celery</li>
<li>Cucumber</li>