JavaScript - Loop
http://webapplog.com/breaking-bad-loops-in-javascript-libraries/
How can we loop over the properties of an object?
Use the 'for in' loop:
var obj = {one: 1, two: 2, three: 3};
for (var p in obj) {
// do something
}
for (myvar in obj) {
if (obj.hasOwnProperty(myvar)) {
// ..
}
}
On each iteration, another property name string from the object is assigned to the variable myvar. The hasOwnProperty method is used to determine whether the property name is truly a member of the object or was found instead on the prototype chain. Think carefully when you use the hasOwnProperty method.
page revision: 2, last edited: 25 Nov 2021 07:26