5

I have created an object person and created 2 properties for that person object in JavaScript. I passed data to that object as:

personA = new person("Michel","Newyork"); 
personB = new person("Roy","Miami"); 

What I need is, how to display both personA & personB values at same time through JavaScript?

4
  • 3
    Where do you want to display the values? And what do you mean by "at the same time"? Commented May 17, 2013 at 13:43
  • What does the person constructor take? What property does it assign too? Commented May 17, 2013 at 13:44
  • Show us the code for person! Commented May 17, 2013 at 13:58
  • For display object value Please refer this link technologiessolution.blogspot.in/2013/11/… Commented Nov 28, 2013 at 6:28

3 Answers 3

2

If you simply want to display them on the console,for debug purposes, use

 console.log (personA, personB);

If you want to alert them to the screen :

 alert (JSON.stringify (personA), JSON.stringify (personB));

If you want to change a DOM element to contain the values :

 domElement.innerHTML = personA.name + ' from ' + personA.loc + ' and ' +
                        personB.name + ' from ' + personB.loc;

assuming here than name and loc are the property names you used.

Of course you can use any of these methods in any context depending on your requirements.

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

Comments

1

I'm going to assume person takes a name and loc (for location) since you didn't clarify:

var personArr = []; //create a person array
//Then push our people
personArr.push(personA);
personArr.push(personB);
for (var i = 0; i < personArr.length; i++) {
   console.log(personArr[i].name);
   console.log(personArr[i].loc);
}

If you're talking about literally at the same time, I won't say it's impossible, just not possible with JavaScript unless you use WebWorkers and those don't fair too well in IE.

1 Comment

Not a problem! If it helped, would you mind selecting the answer as selected?
1

You could also consider this approach:

var persons = {
    person: [],

    add: function(name, city)
    {
        this.person.push({
            'name': name,
            'city': city
        });
    }

};

persons.add("Michael", "New York");
persons.add("Roy", "Miami");

Output:

for(x in persons.person)
{
    console.log(x + ":");

    for(y in persons.person[x])
    {
        console.log(y + ": " + persons.person[x][y]);
    }
}

------

0:
name: Michael
city: New York
1:
name: Roy
city: Miami

Comments