3

i have a problem. I have written a code for extend Array Element, and works fine, but when i iterate over array this show extended functions. I don't know how stop this. There is the code...

Array.prototype.remove  = function(e)   {var i = this.inArray(e);if(i != -1) this.splice(i, 1);return this;};
Array.prototype.add     = function(e)   {this.push(e); return e;};
Array.prototype.inArray = function(v)   {for(i in this) if(v==this[i])return i;return false;};
Array.prototype.toggle  = function(v)   {this.inArray(v) ? this.remove(v) : this.add(v);return this;};

So when i tried this...

var arr = [1,2,3,4,5];
for(i in arr)
document.write(arr[i]);

this print array values and functions extended. somebody can help me? I can't change the code "for(x in y)" because is many times in many files.

6
  • You're trying to iterate over the values in the array, but it's iterating over your custom functions as well? Commented Aug 15, 2011 at 23:42
  • You should not use for..in with arrays because the order of enumeration is implementation dependent. Most browsers will return numeric keys first in lowest to highest order, but IE will return keys in the order they are added. So you will get lowest to highest only if that is the order in which they were added. e.g. `var i=2, a=[], p;while(i--)a[i] = i;for(p in a) alert(p);' shows 0, 1 in most browsers but 1, 0 in IE. Commented Aug 15, 2011 at 23:56
  • 1
    "I can't change the code..." Sure you can, and you should. The cost of writing faulty code is fixing faulty code. Commented Aug 15, 2011 at 23:58
  • 1
    [Waves hand] "You will fix the faulty JavaScript." OP replies: "I will fix the faulty JavaScript." Commented Aug 16, 2011 at 0:02
  • 1
    @patrick Ta kur pe je lax ne punu isok! Commented Aug 16, 2011 at 4:28

4 Answers 4

2

Couple of things to read that will explain the situation

https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

Use .forEach() when iterating over an array. It's pretty well supported, including Mobile Safari and Android, https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach

Sign up to request clarification or add additional context in comments.

2 Comments

forEach() isn't widely supported yet, but it is a good option if your target platforms support it.
It's pretty well supported The only IE that supports it is is IE9. For most public facing websites that is a bit of an issue (unless you monkey patch it, the MDN docs have a good example).
1

If you must modify the Array prototype, you must use hasOwnProperty() otherwise it will pick up properties up the prototype chain.

var arr = [1,2,3,4,5];
for(var i in arr) {
    if (arr.hasOwnProperty(i)) {
        document.write(arr[i])
    }
}

You said, however, you don't want to change your for (in) loops. Why don't you have an Array utility object? Or just use normal for loops? These are Arrays right? for (in) is for iterating over Object properties.

Comments

0

for ... in loops through object properties in JavaScript, not array elements. The for ... in loops are simply the wrong way to do this. You've said that you can't change them, but using this syntax to enumerate the contents of arrays is not a recommended practice in JavaScript, for the reason you have just discovered.

You are going to have to rewrite the for ... in loops sooner or later.

2 Comments

Arrays are objects, for..in will loop over all array properties, including the ones with numeric names.
Exactly. Which means that using for ... in as though it were foreach from other languages is wrong.
0

Iterate through the array's indexes.-

var A=[1,2,3,4,5];

Array.peototype.omega=function(replacer){
  var L= this.length;
  if(L)--L;
  if(replacer!=undefined)this[L]=replacer;
  return this[L]
}

for(var i=0,L=A.length; i<L; i++){
//do something to A[i]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.