lundi 27 juillet 2015

Access nth item when iterating Ember object array with for rather than forEach

It made more sense in a particular case today to use a standard for (i = 0.. loop rather than a forEach, but I realised I don't know how to access objects of an Ember Array by number.

So lets say we have:

var order = this.get('order');
var orderItems = order.get('orderItems');

orderItems.forEach(function(orderItem) {
  orderItem.set('price', 1000);
});

I thought I could do the equivalent as:

var order = this.get('order');
var orderItems = order.get('orderItems');

for (i = 0; i < orderItems.get('length'); i++) {
  orderItems[i].set('price', 1000);
}

but I get orderItems[0] is undefined etc.

How do I access the nth element in an ember array in js?




Aucun commentaire:

Enregistrer un commentaire