Javascript counting number of objects in object

0 votes

I have an object something like:

Object {0=Object, 1=Object, 2=Object} // Output from console.log(obj.Data);

But there is no way for me to count the number of objects in the object and then get the attribute value from the subobjects.

I attempted

console.log(obj.Data[0].length); // It does not work

console.log(obj.Data.length); // It does not work

This is a little tricky for me. I'm hoping you can assist.

Nov 15, 2022 in Java by Nicholas
• 7,760 points
639 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The simplest approach to accomplish this while maintaining outstanding performance and compatibility with both old and modern browsers is to add Lo-Dash or Underscore in your website.

Then you may use _.size(object) or _.keys() (object).

length

You might try this with your obj.Data:

console.log( _.size(obj.Data) );

or:

console.log( _.keys(obj.Data).length );

Both Lo-Dash and Underscore are good libraries that you would find quite handy in your programming. (They are quite similar; Lo-Dash is a newer version with certain advantages.)

You may also include the following method in your code, which just runs over the object's properties and counts them:

function ObjectLength( object ) {
    var length = 0;
    for( var key in object ) {
        if( object.hasOwnProperty(key) ) {
            ++length;
        }
    }
    return length;
};

You can test this with:

console.log( ObjectLength(obj.Data) );

However, with newer browsers, such code is not as quick as it might be. You may use: for a version that is significantly quicker in newer browsers but still works in older ones.

function ObjectLength_Modern( object ) {
    return Object.keys(object).length;
}

function ObjectLength_Legacy( object ) {
    var length = 0;
    for( var key in object ) {
        if( object.hasOwnProperty(key) ) {
            ++length;
        }
    }
    return length;
}

var ObjectLength =
    Object.keys ? ObjectLength_Modern : ObjectLength_Legacy;

and as before, test it with:

console.log( ObjectLength(obj.Data) );

In contemporary browsers, this code utilises Object.keys(object).length; in older versions, it goes back to counting in a loop.

But if you're going to do all of this effort, I'd advocate utilising Lo-Dash or Underscore instead to reap the benefits of those libraries.

answered Nov 17, 2022 by Damonlang
• 700 points

edited Mar 5

Related Questions In Java

0 votes
0 answers

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

I'd want to transform the following object: {"1":5,"2":7,"3":0,"4" ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
1,308 views
0 votes
0 answers

creating list of objects in Javascript

Is it feasible to make a list ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
627 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
5,041 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,920 points
3,009 views
+1 vote
6 answers

How to separate digits of a number in Java ?

String numberString = Integer.toString(number);  for (int i = ...READ MORE

answered Dec 5, 2023 in Java by Akash
• 100 points

edited Mar 5 212,574 views
0 votes
1 answer

How to get the number of digits in an int?

You can find out the length of ...READ MORE

answered May 14, 2018 in Java by Akrati
• 3,190 points
1,074 views
0 votes
0 answers

Javascript counting number of objects in object

I have an object something like: Object {0=Object, ...READ MORE

Nov 17, 2022 in Java by Nicholas
• 7,760 points
674 views
0 votes
0 answers

Convert Array to Object

How can I convert this : ['a','b','c'] To this: { ...READ MORE

May 19, 2022 in Java-Script by Kichu
• 19,040 points
881 views
0 votes
0 answers

Convert Array to Object

What is the best way to convert: ['a','b','c'] to: { ...READ MORE

Sep 20, 2022 in Java by Nicholas
• 7,760 points
798 views
0 votes
0 answers

What is property in hasOwnProperty in JavaScript?

Consider: if (someVar.hasOwnProperty('someProperty') ) { // Do something(); } ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
903 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP