Implementing Array functions (indexOf, lastIndexOf, removeDuplicates...)
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 == 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 == this
) { 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);
}
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 == value) return i;
}
return -1;
}
});
But..... Object.defineProperty() is missing in ExtendScript too!
I don't know what to try next... Any idea?
Thanks!
