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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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
Another option would be to use vanilla Javascript using the
Object.keys()and the Array.map()functions like thisSee 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
jQueryorObject.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
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
hasOwnPropertyfunction like soSee https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty for more information.
The
jQuery.eachandObject.keysfunctions 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.loglike this: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 thebuttonIdvariable at that moment. And since the loop has finished at that moment, thebuttonIdvariable will still be “button2” (the value it had during the last loop iteration), and somessagesByButtonId[buttonId]will bemessagesByButtonId["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.eachandObject.keys().map()solve this problem automatically for us, because it provides us with afunction(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.