am trying to loop and get the values of names from this array , but am not able..really frustrated with javascript
can anyone please help and guide me to do this and for more complex arrays.. i cant seem to find and tutorial good to show examples of this
thank you , here is the code
var object={name:'angelos',name:'nick',name:'maria'};
var i;
for (i = 0; i < object.length; i += 1) {
document.writeln(object[name][i]);
}
First of all, your object has duplicate keys
name. This is poor code and will throw an error in strict mode.I would also use either a
for ... inloop orArray.forEachhere, because much less code is required to implement the desired effect.Seems like you need to use an Array:
You can use
Array.forEach, which passes in each index to an anonymous function.Alternatively, if you wanted each person to be an
Object:References
Array.forEachherefor ... inloops here