If I'm not mistaken, when a section refers to an object, the properties of the object are exposed inside the section, but there is not iteration. So in the section {{#main}}, arr1 and arr2 are exposed, and {{#arr1}} would iterate the array.
Mustach loop through Obj containing Array containing Obj
-
15-07-2023 - |
Question
I have an Object containing Arrays containing Objects
Example:
main: {
arr1: [
obj1: {x: 0, y: 0},
obj2: {x: 0, y: 0}
],
arr2: [
obj3: {x: 0, y: 0},
obj4: {x: 0, y: 0}
]
}
I need with Mustach to retrieve all the x values of those objects (obj1 - 4).
Every object and array can have a random name.
I tried:
{{#main}}
{{#.}}
{{x}}
{{/.}}
{{/main}}
But it's not working.
Anyone have an idea?
Thanks
Solution
OTHER TIPS
None of those things in your example are arrays, they're all objects. Arrays have 0-indexed consecutive numeric keys. That's all you can iterate over in Mustache.
If you want to iterate over the values of objects, you should prepare your data before passing it off to Mustache. Something like Underscore.js's _.values
function is super useful for that.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow