3

I use for arrays : myArray.0 which works fine. But when it's an object myObject.0 doesn'twork.

Here is an example of the object I want.

myObject : {
    book1 : {
        title:"One",
        Author:"Someone",
        Date:"19/06/13",
    },
    book2 : {
        title:"Two",
        Author:"Someone else",
        Date:"12/02/10",
    },
    book3 : {
        title:"Three",
        Author:"Another someone",
        Date:"03/09/03",
    }
}

I want to do something like :

`<h2>{{myObject.0}}</h2>` 

and I expect

<h2>book1</h2>

Any idea of how I can do it ?

Thanks

Ambroise Collon
  • 3,839
  • 3
  • 18
  • 37

2 Answers2

9

try this:

{{#with myObject.[0]}}   
    ..what ever you want to do with that object in here
{{/with}}

In this scenario you must include [ ] brackets.

However, if you are trying to access a property of an object the square brackets are not required

myObject.0.objectProperty
user2590928
  • 176
  • 10
2

The reason you can't do this is that the order of properties in an object is not necessarily preserved. So, although you wrote them as book1, book2, book3, if the syntax you suggest were allowed then myObject.0 might return any of the 3 books.

See Does JavaScript Guarantee Object Property Order? for a little more depth on this.

Community
  • 1
  • 1
Michael B
  • 106
  • 4