最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display object values - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited May 17, 2013 at 13:45 SomeShinyObject 7,8216 gold badges40 silver badges60 bronze badges asked May 17, 2013 at 13:40 user2394076user2394076 4
  • 3 Where do you want to display the values? And what do you mean by "at the same time"? – Dek Dekku Commented May 17, 2013 at 13:43
  • What does the person constructor take? What property does it assign too? – SomeShinyObject Commented May 17, 2013 at 13:44
  • Show us the code for person! – Bergi Commented May 17, 2013 at 13:58
  • For display object value Please refer this link technologiessolution.blogspot.in/2013/11/… – Nikunj K. Commented Nov 28, 2013 at 6:28
Add a ment  | 

3 Answers 3

Reset to default 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.

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.

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
发布评论

评论列表(0)

  1. 暂无评论