Search code examples
ember.jshandlebars.js

Passing an array from a template to a helper function


Using Ember, I'm attempting to pass an array from a template

{{segment-array images 5}}

to a helper function

export function segmentArray([array, itemsPerRow]) {

  /* Logic to create and a return multidimensional array */

}

I know images is being populated correctly as I can use the {{#each}} helper on it. However, array is being passed in as "<(subclass of Ember.ArrayProxy):ember355>" with no length or content.

How can I pass in, modify, and return an array?


Solution

  • The ArrayProxy has length and content, just don't use dot notation but Embers .get():

    Ember.get(array, 'length');
    

    But if you want to simply loop you can use .forEach:

    array.forEach((item, index) => {
      ...
    });
    

    also you can use the for of loop:

    for (let item of array){
      ...
    }