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 plex 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]);
}
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 plex 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]);
}
Share
Improve this question
asked Aug 1, 2012 at 14:46
anjelanjel
1,3924 gold badges23 silver badges36 bronze badges
2
- your array is set up wrong. To do what you want you should have var object =[{name:'angelos'},{name:'nick'},{name:'maria'}] and then can iterate over object[i]['name'] – Ben McCormick Commented Aug 1, 2012 at 14:49
- AFAIS there is only one property name: 'maria' on your object. THe last one overrules all previous values on name. var object={name:'angelos',name:'nick',name:'maria'}; is equivalent to var object={name:'maria'}; – Ashwin Prabhu Commented Aug 1, 2012 at 14:53
5 Answers
Reset to default 6That's an object, not an array. You can make it a simple array instead:
var arr = ['angelos', 'nick', 'maria'];
for (var i = 0; i < arr.length; i++) {
document.writeln(arr[i]);
}
Or, if you want to have objects inside the array (not needed if every object has just one key):
var arr = [{name: 'angelos'}, {name: 'nick'}, {name: 'maria'}];
for (var i = 0; i < arr.length; i++) {
document.writeln(arr[i].name);
}
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 ... in
loop or Array.forEach
here, because much less code is required to implement the desired effect.
Seems like you need to use an Array:
var arr = ["nick", "maria", "chaz"];
arr.forEach(function (name) {
document.writeln(name);
});
You can use Array.forEach
, which passes in each index to an anonymous function.
Alternatively, if you wanted each person to be an Object
:
var people = [{name: 'chaz', title: 'mr'}, {name: 'nick', title: 'mr'}, {name: 'maria',title: 'ms'}];
for (i in people) {
if (!people.hasOwnProperty(i)) { continue; }
var person = people[i];
document.writeln(person.name);
}
References
- Take a look at
Array.forEach
here - A good reference on
for ... in
loops here
You can put your data in an array and fill it with objects containing a name attribute (and others e.g. adress or so, if you like to)
http://jsfiddle/5NK6x/
var obj=[{name:'angelos'}, {name:'nick'}, {name:'maria'}],
i;
for (i = 0; i < obj.length; i += 1) {
document.write(' ' + obj[i]['name']);
}
First of all, that is an object, not an array. You probably meant to have an array of objects. I'm saying that because you have three keys all called name
. Keys must be unique. like this:
var people = [{name: 'angelos'}, {name:'nick'}, {name:'maria'}];
In that case you would loop through like this:
for (var i = 0; i < people.length; i++) {
document.writeln(people[i].name);
}
Example: http://jsfiddle/lbstr/cMqaH/
This is a mix between an array and JSON. If your data looked like this:
var object = [{"name":"angelos"},{"name":"nick"},{"name":"maria"}];
You'd be able to access the elements like so:
for(var i=0,i<object.length,i++)
{
alert(object[i].name);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745598948a4635284.html
评论列表(0条)