Access model data as an array


#1

Is there any way of access the data from a model (like name, surname, etc.) in a array way instead of specifying the name of its attributes?

I mean, something like this:

model(0)

instead of

model.name

#2

Any model in Ember is an object. How do you imagine accessing an object property by its “number”? There’s no such thing as the “first” or any other key in the object. They are unordered by deign and all that properties may be sorted by browser.

I guess my best tip here is to use an array of object keys. Something like this:

var keys = Object.keys(model);
var key = keys[0];
var prop = model[key];

But I’d rather obtain a property by its name, it’s the only right answer here.


#3

Make a mistake - fix a mistake.

Look what I’ve found in API: http://emberjs.com/api/data/classes/DS.Model.html#property_fields

Thus you actually can do that like this

var key = model.fields[0];
var prop = model[key];

Sorry for messing up.


#4

Don’t worry @od1n, I’m learning so any help, even if it’s a correction to my understandings is great for me :slight_smile: thank you very much for your help, both approach are very interesting!


#5

The documentation for Ember arrays is here: http://emberjs.com/api/classes/Ember.Array.html

Sample methods:

.firstObject();
.lastObject();
.objectAt(0);
.forEach();