5

Often, I need to loop through an Ember.ArrayProxy object's content.

Exemple 1, I need to build a list of IDs:

var loc = myArrayProxy.get('length') || 0,
    ids = new Array(),
    idsList;

while(--loc >= 0) {
    var curObject = myArrayProxy.objectAt(loc);
    ids.push(curObject.id);
}
idsList = ids.join(',');

Exemple 2, I need to build an Array of primitive objects (not Ember.Object):

var loc = myArrayProxy.get('length') || 0,
    newContent = new Array();

while(--loc >= 0) {
    var curObject = myArrayProxy.objectAt(loc);
    newContent.push({
                      id:   curObject.id,
                      name: curObject.name
                   });
}

Question: is there a better way to do this? The "while(--loc >= 0)" seems bad to me.

1
  • Now using "getEach()" and "forEach()". The documentation really lacks examples... Thanks :) Commented Jul 5, 2012 at 12:38

1 Answer 1

8

Ember.ArrayProxy provides many friendly functions (through Ember.Array, Ember.Enumerable, ...). Loops can often be avoided using "forEach". In your 2nd example, you may consider using "map". Here is a link to Ember.ArrayProxy documentation. Be sure to look at: Ember.Array and Ember.Enumerable

edit:

For instance, assuming the order of the ids is not relevant, your first example could be written:

var idsList = myArrayProxy.mapProperty('id').join(',');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.