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?
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.
Douglas Crockford recommends in JavaScript: The Good Parts to avoid using the
for instatement.If you use
for into loop over property names in an object, the results are not ordered.The
for inloop is best for iterating over name-value pairs, and thefor eachloop best for iterating over values i.e arrays.E.g,
There’s no way we can get the property name if we were to use the For Each Loop for the above object.
Note :