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

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

Community Beginner ,
Aug 13, 2024 Aug 13, 2024

Copy link to clipboard

Copied

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();

 

TOPICS
Scripting

Views

228

Translate

Translate

Report

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

correct answers 1 Correct answer

Adobe Employee , Aug 13, 2024 Aug 13, 2024

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

 

 

Votes

Translate

Translate
Community Beginner ,
Aug 13, 2024 Aug 13, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Adobe Employee ,
Aug 13, 2024 Aug 13, 2024

Copy link to clipboard

Copied

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

 

 

Votes

Translate

Translate

Report

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
Community Expert ,
Aug 13, 2024 Aug 13, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Enthusiast ,
Aug 13, 2024 Aug 13, 2024

Copy link to clipboard

Copied

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, '');
}

 

Votes

Translate

Translate

Report

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
Community Beginner ,
Aug 13, 2024 Aug 13, 2024

Copy link to clipboard

Copied

LATEST

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!

Votes

Translate

Translate

Report

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