Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Contributor ,
Dec 30, 2015 Dec 30, 2015

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.

TOPICS
Scripting
578
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Dec 31, 2015 Dec 31, 2015

Hi,

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

Jarek

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 01, 2016 Jan 01, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 01, 2016 Jan 01, 2016
LATEST

Right Marc,

One can't jump over this.

Jarek

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines