Frage

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

War es hilfreich?

Lösung

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.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top