1

Sorry if my question is bad. I'm stuck in the following simple javascript code:

var person={fname:"John",lname:"Doe",age:25};

for (x in person)
{
document.write(person[x] + " ");
}

The above code result is : John Doe 25

How do you show only value from lname element ? (with for (... in ...) statement)

I don't want to use person={"John","Doe",25} instead of one of above. I want to know how to access the element from array to get value.

Thank you

Edit: Thanks all for responses. This question was created because I have many items in array like :

        [{Name="A 1", Email="[email protected]", Group="Group1"},
        {Name="A 2", Email="[email protected]", Group="Group2"},
        {Name="A 3", Email="[email protected]", Group="Group3"},
        {Name="B 1", Email="[email protected]", Group="Group1"},
        {Name="V 1", Email="[email protected]", Group="Group2"},
        {Name="X 1", Email="[email protected]", Group="Group4"},
        {Name="Y 1", Email="[email protected]", Group="Group3"},
        {Name="Z 1", Email="[email protected]", Group="Group3"},
        {Name="W 1", Email="[email protected]", Group="Group6"}]

I want to iterate this array and compare the element with my owned value. if object1.Group == 'Group3' { code }

2
  • Do you mean using person.lname? What do you expect the result to be? Commented Dec 21, 2011 at 7:28
  • I want to write lname on the page... for example Doe instead of John Doe 25 Commented Dec 21, 2011 at 7:31

4 Answers 4

5

What you have here is not an array, it is a JavaScript object. (In simple terms,) Arrays have numeric indexes, while objects have properties with property-name and value pairs.

To access a specific object property like "lname" simply say:

person.lname
// OR
person["lname"]

Or, if the name of the property you want to use is in a variable:

var whichProp = "lname";
person[whichProp]

With your data, to produce the output "John Doe" you'd say:

document.write(person.fname + " " + person.lname);

The for (x in person) syntax that you were using loops through every property of the person object, setting x to each property name in turn.

UPDATE FOR YOUR UPDATED QUESTION:

Assuming you have an array of objects:

var people = [{Name:"A 1", Email:"[email protected]", Group:"Group1"},
              {Name:"A 2", Email:"[email protected]", Group:"Group2"},
              {Name:"A 3", Email:"[email protected]", Group:"Group3"},
              // etc ];

And you want to find the element where Group is "Group3" then you loop through the array with a traditional for loop as follows:

for (var i=0; i < people.length; i++) {
   if (people[i]["Group"] === "Group3") {
      // do something
   }
}

Within the if statement you can add a break statement if you want to stop processing after the first match. And of course you are free to use the people[i].Group syntax rather than people[i]["Group"] if you prefer it.

(Note also that your object literal syntax was incorrect in your question update: where you've used = you should've used : as shown in my answer above.)

Sign up to request clarification or add additional context in comments.

3 Comments

Good explanation of all cases. @Michael See my example if you want to have an array of multiple person objects
What happens if I want to iterate the big array (see edited code above) and compare element ?
@MichaelSwan - I've updated my answer to cover your new requirement.
1
var arr = [];
var person={fname:"John",lname:"Doe",age:25};
arr.push(person);

for (var i=0; i < arr.length; i++)
{
   document.write(arr[i].lname+ " ");
}

Update

JsFiddle example

1 Comment

@MichaelSwan could you solve your problem then or is further assistance needed?
1

You can access the two properties fname, lname of the person object like this:

document.write(person.fname + " " + person.lname);

1 Comment

@MichaelSwan the code above assuming that the person is an array, if it is not an array try it without an index like : person.fname, person.lname see my edit
1

That's not an Array, that's an Object. If you know the property you want to access, you don't need to iterate the Object. Just reference the property directly.

person.lname;

Edit: To iterate your Array and take action when the group matches:

for (var i = 0; i < objects.length; i++) {
    if (objects[i].Group == "Group3") {
        // do something 
    }
}

But, the code you posted is invalid. Property/value pairs are separated with a :, not =.

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.