Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8123199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:06:00+00:00 2026-06-06T06:06:00+00:00

According to the way to add indexOf method to Array class in IE6 ,

  • 0

According to the way to add indexOf method to Array class in IE6, how do I now reject this method from iterating through any random array? For example:

Array.prototype.indexOf = function(needle) { ... };

var array = [1, 2, 3];
for (var i in array) {
    document.write(i + ': ' + array[i]);
}

gives output

0: 1
1: 2
2: 3
indexOf: function ...

How can I skip indexOf property and stop iterating on it without adding any code to where

for(...)

is called?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:06:02+00:00Added an answer on June 6, 2026 at 6:06 am

    This is a well known issue with Javascript’s for..in loops.

    You may think you’re just looping through the elements of an array or an object that you’ve added directly, but it will also loop through any methods in the prototype as well. This can have unexpected consequences.

    There are two ways around it:

    Firstly, for arrays, don’t use for..in. Use a simple for() loop instead — ie:

    for(var counter=0; counter<myArray.length; counter++) {...}
    

    This form is recommended for arrays, because it is guaranteed to only iterate through the numeric array elements.

    It doesn’t work for generic objects, of course, since they don’t have a length property nor numeric elements. For these, you still need to use for..in, but you should also always include an if() statement inside a for..in loop to filter out unwanted elements.

    The format of this should look like this:

    for (name in obj) {
        if (obj.hasOwnProperty(name)) {
            ...
        }
    }
    

    This looks ugly, but because of the problems of not doing it, it is recommended best-practice for Javascript, to the point that JS code quality cheking tools such as JSLint and JSHint will flag it up as a problem if you don’t write your for..in loops like this.

    You can read more about this problem here: http://yuiblog.com/blog/2006/09/26/for-in-intrigue/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to Best way to remove from NSMutableArray while iterating? , we can't remove
According to the documentation : There is no way to block signals temporarily from
I'm wondering if there's any way for a Rails asset to vary according to
Is there a way add another dropdown form to this script? http://javascript.internet.com/navigation/connected-comboxes.html Or do
Is there any any way to add a TextView to application settings in Iphone.
Is there any way to add an activity into another activity?. I think TabHost
Is there a way to add a category to a class whose header file
I'm building my project with maven so according to maven way, config should be
According to the class ElementType in the API, you can annotate local variables and
How to add html tags according to the div width? Php code to output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.