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 9222589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:52:06+00:00 2026-06-18T03:52:06+00:00

I am currently using two kinds of iterations in my code: For objects (snippet

  • 0

I am currently using two kinds of iterations in my code:

For objects (snippet 1):

for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        // do stuff with key and obj[key]
    }
}

For arrays (snippet 2):

for (var i=0, count=arr.length;i<count;i++) {
    // do stuff with i and arr[i]
}

I’d like to simplify my code and use a single function for both objects and arrays (snippet 3):

var keys=Object.keys(arrorobj);
for (var i=0, count=keys.length;i<count;i++) {
    // do stuff with keys[i] and arrorobj[keys[i]]
}

I know that the snippet 3 is fine for objects, but is it also going to work as I expect with arrays, and replace both snippets 1 and 2? In particular, can I be assured that in all browsers (well, at least those that support Object.keys) snippet 3 will respect the key order (up from index 0)?

[edit] I plan to use this in particular for deep merge of objects. My current code is heavy as at each level I have 3 branches depending on the type (array, object or other).

  • 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-18T03:52:07+00:00Added an answer on June 18, 2026 at 3:52 am

    It will work while the only enumerable properties on your array are the numbered slots (the default of an Array), but it doesn’t make a guarantee of the ordering of the returned properties, as they’re defined to be in for ( in ) order, which doesn’t always iterate numerically (depending on how the properties were assigned).

    If you assigned another property, it’d also be enumerated (this wouldn’t happen with a more standard iteration technique).

    It also wouldn’t behave quite the same for empty slots (e.g. new Array(25)), as the for loop would iterate over these.

    It is generally recommended to use a for loop for an Array, or forEach() method (or related).

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

Sidebar

Related Questions

I'm running SourceSafe and I have two branches of my code. I'm currently using
I'm currently using two libraries (prototype and jQuery), thus I've implemented the jQuery noConflict
I am currently using two catch blocks after a try block. The first catches
I am currently using two .htaccess files for my website: one is the development
I currently am using two stylesheets to get my intended output. The first stylesheet
I have an application in which I'm currently using a two column table in
Currently using Xcode 4.2 and I have two view controllers (1 and 2). I
I am currently using Symfony2 and Doctrine2 and am trying to join two tables
To narrow it down: I'm currently using Boost.Unordered . I see two possible solutions:
I'm currently designing a web application using php, javascript, and MySQL. I'm considering two

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.