Skip to main content
wjonsmith
Participant
Aug 13, 2024 9:23 PM UTC
Answered

AE Scripting: Array methods (forEach, IndexOf) not working in 2024?

Scripts that are working in 2023 are not working for me in 2024. It appears to be some javascript array methods that are causing the issue. I wrote a simple script to test and am getting the same results: works fine in 2023, does not work in 2024 or the latest beta. This is on an M2 Macbook Pro. Not sure if this is related to AE itself or something to do with my setup.

 

// testing script
function testForEachMethod() {
    var testArray = [1, 2, 3, 4, 5];
    var result = [];

    // Using forEach to iterate over the array
    testArray.forEach(function(element, index) {
        result.push("Element " + index + ": " + element);
    });

    // Display the result
    alert("forEach results:\n" + result.join("\n"));
}


function testIndexOf() {
    var testArray = [1, 2, 3, 4, 5];

    for (var i = 1; i <= 6; i++) {
        // Loop through numbers 1-6 and use indexOf to determine if that number is in the array
        if (testArray.indexOf(i) === -1) {
            alert("Value " + i + " is not in the array.");
        } else {
            alert("Value " + i + " is in the array.");
        }
    }
}

testForEachMethod();
testIndexOf();

 

This topic is closed to new replies. Start a new post to keep the conversation going.
Douglas_WaterfallCorrect answer
Adobe Employee
Aug 13, 2024 9:43 PM UTC

Array.forEach is not a native ExtendScript function, so I am guessing that you were getting it for free because some other script you were using (might be a startup script) added the polyfill to the global space.

 

You can find it here

 

Douglas Waterfall

After Effects Engineering

 

 

5 replies

Legend
Aug 13, 2024 10:56 PM UTC

I use these polyfill in Extendscript:

polyfill.js

Array.prototype.every = function(callback) {
  for (var i = 0; i < this.length; i++) {
    if (!callback(this[i], i, this)) {
      return false;
    }
  }
  return true;
}

Array.prototype.filter = function(callback) {
  var arr = [];
  for (var i = 0; i < this.length; i++) {
    if (callback.call(this, this[i], i, this)) {
      arr.push(this[i]);
    }
  }
  return arr;
}

Array.prototype.forEach = function(callback) {
  for (var i = 0; i < this.length; i++) {
    callback(this[i], i, this);
  }
}

Array.prototype.indexOf = function(item) {
  var index = 0,
    length = this.length;
  for (; index < length; index++) {
    if (this[index] === item)
      return index;
  }
  return -1;
}

Array.prototype.map = function(callback) {
  var arr = [];
  for (var i = 0; i < this.length; i++) {
    arr.push(callback(this[i], i, this));
  }
  return arr;
}

Array.prototype.reduce = function(callback, initialValue) {
  var accumulator = initialValue;
  for (var i = 0; i < this.length; i++) {
    if (accumulator !== undefined) {
      accumulator = callback.call(undefined, accumulator, this[i], i, this);
    } else {
      accumulator = this[i];
    }
  }
  return accumulator;
}

Array.prototype.some = function(callback) {
  for (var i = 0; i < this.length; i++) {
    if (callback(this[i], i, this)) {
      return true;
    }
  }
  return false;
}

String.prototype.trim = function() {
  return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
}

 

Translate
wjonsmith
wjonsmithAuthor
Participant
Aug 14, 2024 12:04 AM UTC

Ahhhh this explains it. Thanks for all the answers. After a little more digging I ran into this thread which expands on this even further. Some plugins and panels are loading polyfills as Douglas mentioned. Even opening the "Learn" panel loads Array.indexOf. Problem solved!

Translate
Dan Ebberts
Community Expert
Community Expert
Aug 13, 2024 9:52 PM UTC

It deosn't work for me in 2023 or 2024. Are you sure it wasn't an expression where you got forEach() to work with the JavaScript engine? I would think that it would never have worked with scripting based on the version of ECMAScript that AE scripting is based on.

 

(edit):

Ah, Douglas' explanation makes more sense.

Translate
Douglas_WaterfallCorrect answer
Adobe Employee
Aug 13, 2024 9:43 PM UTC

Array.forEach is not a native ExtendScript function, so I am guessing that you were getting it for free because some other script you were using (might be a startup script) added the polyfill to the global space.

 

You can find it here

 

Douglas Waterfall

After Effects Engineering

 

 

Translate
wjonsmith
wjonsmithAuthor
Participant
Aug 13, 2024 9:29 PM UTC

I wanted to add that I'm on macOS Ventura 13.6.4.

Translate