Count Records in Javascript Object - Stack Overflow

I have a javascript object and I would like to count the number of records in that object. I have tried

I have a javascript object and I would like to count the number of records in that object. I have tried length and size however I am not getting a count.

Object

 var person = new Object();
    person.name = null;
    person.age = null;
    person.gender = null;

I then populate this Object with data like;

person.name = 'John';
person.age = 20;
person.gender = 'M';

person.name = 'Smith';
person.age = 22;
person.gender = 'M';

I would like to return a count with two rows of data.

I have a javascript object and I would like to count the number of records in that object. I have tried length and size however I am not getting a count.

Object

 var person = new Object();
    person.name = null;
    person.age = null;
    person.gender = null;

I then populate this Object with data like;

person.name = 'John';
person.age = 20;
person.gender = 'M';

person.name = 'Smith';
person.age = 22;
person.gender = 'M';

I would like to return a count with two rows of data.

Share Improve this question edited Oct 9, 2013 at 3:58 dcaswell 3,1672 gold badges27 silver badges25 bronze badges asked Oct 9, 2013 at 3:52 devdardevdar 5,65428 gold badges104 silver badges156 bronze badges 9
  • What do you mean by number of records in the object? – PSL Commented Oct 9, 2013 at 3:55
  • 1 Your object doesn't contain two records, it contains three properties which you overwrite with the second set of values. Objects don't have a length (unless you add one yourself); what you need here is an array of objects, not a single object. – nnnnnn Commented Oct 9, 2013 at 3:56
  • See here for more information: stackoverflow./a/1345992/1861269 – zombiehugs Commented Oct 9, 2013 at 3:56
  • 1 Do you mean to say that you have a multi dimensional array of objects of type person? – PSL Commented Oct 9, 2013 at 3:58
  • 1 The object as shown in the question is not an array, multidimensional or otherwise. If you have another variable that is an array please add details of that to your question. – nnnnnn Commented Oct 9, 2013 at 4:00
 |  Show 4 more ments

2 Answers 2

Reset to default 8

What you want is an array of objects:

var people = [
    { name: "John",  age: 20, gender: "M" },
    { name: "Smith", age: 22, gender: "M" }
];

From this you can get the length off the array:

people.length; // 2

And you can grab specific people based on their index:

people[0].name; // John

You are simply changing the same object. So the count will always be 1. If you want to get number of objects define a array and assign each object to that. Then you can get the count.

var perarray= new Array();
var person = new Object();
    person.name = null;
    person.age = null;
    person.gender = null;    
person.name = 'John';
person.age = 20;
person.gender = 'M';

perarray[0]=person;

person.name = 'Smith';
person.age = 22;
person.gender = 'M';
perarray[1]=person;

alert(perarray.length);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743724092a4496318.html

相关推荐

  • Count Records in Javascript Object - Stack Overflow

    I have a javascript object and I would like to count the number of records in that object. I have tried

    10天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信