Is there a better way of performing array functions on a collection
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi,
Each collection has availabled method .getElements() ==> which returns an array of elements
Jarek
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Right Marc,
One can't jump over this.
Jarek

