Quantcast
Channel: Adobe Community : Discussion List - After Effects Scripting
Viewing all articles
Browse latest Browse all 2143

Implementing Array functions (indexOf, lastIndexOf, removeDuplicates...)

$
0
0

Hi,

 

 

I'm trying to implement some functions to manipulate Arrays more easily, like I would do with other programming languages (Array.indexOf() does exist in Javascript, but seems to be missing from ExtendScript).

 

First, I tried to add those functions using Array.prototype; here is what I did for two of those functions:

 

Array.prototype.indexOf = function (value)        {          for (var i = 0;i<this.length;i++)            {                if (this[i] == value) return i;            }            return -1;        }      
Array.prototype.removeDuplicates = function ()        {            var removed = [];            for (var i = 0;i<this.length-1;i++) {                    for (var j=i+1;j<this.length;j++) {                        if (this[i] == this[j]) {                                          removed.push(this.splice(j,)1);                        }                    }                }                return removed;        }

 

It seemed to work well, but I found out that it breaks this kind of loop:

 

for (var i in array) {     alert(array[i]);     }

 

The loop goes through array values, and continues beyond array.length with the functions added to the prototype.

 

As explained here, I found a workaround, using Object.defineProperty() instead of implementing functions in Array.proptotype

 

 

Object.defineProperty(Array.prototype, "indexOf", {  enumerable: false,  value: function(value) {      for (var i = 0;i<this.length;i++)            {                if (this[i] == value) return i;            }            return -1;    }
});

 

 

But..... Object.defineProperty() is missing in ExtendScript too!

 

I don't know what to try next... Any idea?

 

Thanks!


Viewing all articles
Browse latest Browse all 2143

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>