0

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]);
    }
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'] 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'}; Commented Aug 1, 2012 at 14:53

5 Answers 5

6

That'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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

It's also not a legal object, so your changes wouldn't be enough to fix this. It's not legal to have a literal object that has duplicate property names.
@Chris I edited that out seconds after I posted my answer (more than 5 minutes ago). Please refresh the page.
3

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

7 Comments

Be aware that this is ECMAScript 5 syntax, and so is unsupported in quite a few current browsers.
Updated, and Array.forEach is easily implemented. Alternatively, in its place, a for...in loop can be used.
What specifically is not working? Do you have a console log or updated code?
am only getting the last value of the array which is maria
With the exact code i posted, should work fine. Try using console.log in place of document.writeLn and look in your console.
|
2

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.net/5NK6x/

var obj=[{name:'angelos'}, {name:'nick'}, {name:'maria'}],
    i;
for (i = 0; i < obj.length; i += 1) {
    document.write(' ' + obj[i]['name']);
}​

Comments

2

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.net/lbstr/cMqaH/

2 Comments

am only getting the last value of the array which is maria
@user1526175 hmm, I'm getting all three. Try the jsfiddle I added to my answer.
0

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);
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.