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

  • Home
  • SEARCH
  • 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 8340503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:11:47+00:00 2026-06-09T05:11:47+00:00

I have object in JavaScript: var object = someobject; Object { aaa=true, bbb=true, ccc=true

  • 0

I have object in JavaScript:

var object = someobject;

Object { aaa=true, bbb=true, ccc=true }

How can I use each for this?

 object.each(function(index, value)) {
      console.log(value);
 }

Not working.

  • 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-09T05:11:48+00:00Added an answer on June 9, 2026 at 5:11 am

    A javascript Object does not have a standard .each function. jQuery provides a function. See http://api.jquery.com/jQuery.each/ The below should work

    $.each(object, function(index, value) {
        console.log(value);
    }); 
    

    Another option would be to use vanilla Javascript using the Object.keys() and the Array .map() functions like this

    Object.keys(object).map(function(objectKey, index) {
        var value = object[objectKey];
        console.log(value);
    });
    

    See https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Object/keys and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

    These are usually better than using a vanilla Javascript for-loop, unless you really understand the implications of using a normal for-loop and see use for it’s specific characteristics like looping over the property chain.

    But usually, a for-loop doesn’t work better than jQuery or Object.keys().map(). I’ll go into two potential issues with using a plain for-loop below.


    Right, so also pointed out in other answers, a plain Javascript alternative would be

    for(var index in object) { 
        var attr = object[index]; 
    }
    

    There are two potential issues with this:

    1 . You want to check whether the attribute that you are finding is from the object itself and not from up the prototype chain. This can be checked with the hasOwnProperty function like so

    for(var index in object) { 
       if (object.hasOwnProperty(index)) {
           var attr = object[index];
       }
    }
    

    See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty for more information.

    The jQuery.each and Object.keys functions take care of this automatically.

    2 . Another potential issue with a plain for-loop is that of scope and non-closures. This is a bit complicated, but take for example the following code. We have a bunch of buttons with ids button0, button1, button2 etc, and we want to set an onclick on them and do a console.log like this:

    <button id='button0'>click</button>
    <button id='button1'>click</button>
    <button id='button2'>click</button>
    
    var messagesByButtonId = {"button0" : "clicked first!", "button1" : "clicked middle!", "button2" : "clicked last!"];
    for(var buttonId in messagesByButtonId ) { 
       if (messagesByButtonId.hasOwnProperty(buttonId)) {
           $('#'+buttonId).click(function() {
               var message = messagesByButtonId[buttonId];
               console.log(message);
           });
       }
    }
    

    If, after some time, we click any of the buttons we will always get “clicked last!” in the console, and never “clicked first!” or “clicked middle!”. Why? Because at the time that the onclick function is executed, it will display messagesByButtonId[buttonId] using the buttonId variable at that moment. And since the loop has finished at that moment, the buttonId variable will still be “button2” (the value it had during the last loop iteration), and so messagesByButtonId[buttonId] will be messagesByButtonId["button2"], i.e. “clicked last!”.

    See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures for more information on closures. Especially the last part of that page that covers our example.

    Again, jQuery.each and Object.keys().map() solve this problem automatically for us, because it provides us with a function(index, value) (that has closure) so we are safe to use both index and value and rest assured that they have the value that we expect.

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

Sidebar

Related Questions

I have javascript object defined like this: function SocialMiner() { var verbose=true; var profileArray=new
I have a Javascript object literal: var Toolbar = { init: function(toolbar) { this.Bar
i have this code in javascript: var object = { get: function(id){ sel =
I have a Javascript object created as follows: var ccStatTracker = (function (){ ccmap:{
I have a javascript that does this (http is your XMLHttpRequest object) var r
I have updated my jSon object using the code <script type=text/javascript > jQuery(document).ready(function($){ var
When I have a JavaScript object like this: var member = { mother: {
I have a JavaScript object that looks something like this: var myTextOptions = {
I have a javascript object that looks somthing like this: var obj = {
I have a javascript object as such (simplified). var MyObject = (function(){ var propertyOne

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.