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 amHow 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 amIf 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 -
Dan Ebberts
October 15, 2022 at 6:34 amI’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 amIf 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)
Reply to this Discussion! Login or Sign Up