Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Compare Arrays in Extendscript

  • Compare Arrays in Extendscript

    Posted by Rodrigo Aben on October 12, 2022 at 4:49 am

    How do I check if an array contains values of another array?

    Lets say I have:

    myLayers: [1,12];

    containingLayers: [1,2,3,4,5];

    How do I check is the second array contains both values of the first one?

    Rodrigo Aben replied 3 years, 7 months ago 3 Members · 5 Replies
  • 5 Replies
  • Dan Ebberts

    October 12, 2022 at 5:26 am

    If you’re using the JavaScript engine, something like this should work:

    myLayers = [1,12];
    containingLayers = [1,2,3,4,5];
    success = true;
    for (var i = 0; i < myLayers.length; i++){
    if (! containingLayers.includes(myLayers[i])){
    success = false;
    break;
    }
    }
    success
  • Rodrigo Aben

    October 15, 2022 at 3:51 am

    Function .includes does not exist on extendscript

  • Dan Ebberts

    October 15, 2022 at 6:34 am

    I’m guessing you either have File > Project Settings > Expressions > Expression Engine set to Legacy Extendscript (instead of JavaScript) or you’re running an old version of AE

  • Filip Vandueren

    October 15, 2022 at 7:05 am

    If you need it in scripting, it’s easier to implement with indexOf(). This is also not in Extendscript, but it is a bit easier to ‘polyfill’:

    if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(obj, start) {
    for (var i = (start || 0), j = this.length; i < j; i++) {
    if (this[i] === obj) { return i; }
    }
    return -1;
    }
    }
    myLayers = [1,12];
    containingLayers = [1,2,3,4,5];
    success = true;
    for (var i = 0; i < myLayers.length; i++){
    if (containingLayers.indexOf(myLayers[i])==-1) {
    success = false;
    break;
    }
    }
    succes;

    (Haven’t tested this)

  • Rodrigo Aben

    October 16, 2022 at 1:21 am

    That solved beautifully, Filip. Thanks a lot!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy