Skip to main content
Inspiring
December 31, 2015
Question

Is there a better way of performing array functions on a collection

  • December 31, 2015
  • 1 reply
  • 634 views

Collections... While having their advantages are a little bit painful as well. Want to pollyfill a lot of modern Javascript functions such as Array.forEach and Array.reduce but as I understand collections are not Arrays so pollyfilling the prototype is not going to help me too much.

I wrote a little function that converts a collection to an array:

/**

* Converts Adobe collection objects to JavaScript arrays

*

* @version 1.0.0

*/

function collectionToArray(col) {

  'use strict';

  var array = [];

  Array.prototype.forEach.call(col, function(itm) {

  array.push(itm);

  });

  return array;

}

It works... It returns an array that I can perform native Javascript functions on. I just wanted to check with the community if this went against what is considered best practices for Adobe scripting.

This topic has been closed for replies.

1 reply

Jump_Over
Legend
December 31, 2015

Hi,

Each collection has availabled method .getElements() ==> which returns an array of elements

Jarek

Marc Autret
Legend
January 1, 2016

Hi,

Each collection has availabled method .getElements() ==> which returns an array of elements

I wouln't say that, Jarek. Each specifier has a getElements() method which returns an array of resolved specifiers (usually handled as DOM objects.)

Then, given a collection myColl, you get the underlying DOM objects as a JS array using myColl.everyItem().getElements().

@+

Marc

Jump_Over
Legend
January 1, 2016

Right Marc,

One can't jump over this.

Jarek