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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:55:56+00:00 2026-05-23T11:55:56+00:00

var myArr = [{a:1, b:2}, {c:3, d:4}]; for (var item in myArr) { console.log(item);

  • 0
var myArr = [{a:1, b:2}, {c:3, d:4}];

for (var item in myArr) {
    console.log(item);
}

Item returns the key (ex: 0, 1) instead of the object itself. Why?

  • 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-05-23T11:55:57+00:00Added an answer on May 23, 2026 at 11:55 am

    Douglas Crockford recommends in JavaScript: The Good Parts to avoid using the for in statement.

    If you use for in to loop over property names in an object, the results are not ordered.

    The for in loop is best for iterating over name-value pairs, and the for each loop best for iterating over values i.e arrays.

    E.g,

    var o = {'name':'Batman', 'age':33, 'city':'Gotham City'};
       for (var p in o) {
            console.log(p+': '+o[p]);
        }
    

    There’s no way we can get the property name if we were to use the For Each Loop for the above object.


    Note :

    1. The For in Loop is best for name-value pairs.
    2. The For Each Loop is best for iterable values. Eg: arrays, and objects if you are not interested in the name of the property.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a function var myarr[] =new Object(); function myfunction(id,msg) { myarr[id,msg] } I
I have an array of object literals like this: var myArr = []; myArr[0]
var $content = $('#SomeDivContainingTwoImages'); $content.children().each(function(i){ $(this).showImage = showImageStatic; $(this).showImage(); }); returns Uncaught TypeError: Object
var foo = { p1: function(){ return this.p2; }, p2: function(){ console.log('i am foo.p2');
var t={a:1,b:2} is this an Object with properties (a,b) or it is a Javascript
I have an Array Collection as follows var myArr:ArrayCollection = new ArrayCollection([ {label: label1,
var myarr= Array('test1','test2','test3'); var searchTerm = test; var rSearchTerm = new RegExp( searchTerm,'i'); $.each(myarr,
I have an array here: var myArr = [1, 1, 2, 5, 5, 7,
I have a JS array that's filled with data as follows: var myarr =
var htm = $.ajax({ type: GET, url: ./viewforms, async: false }).responseText; var myObject =

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.