1

I have an array like this:

[{"this":"that","int":5},{"this":"that","int":5}]

How can I count the number of objects({}) inside of an array with Javascript?

Thanks in advance.

2
  • 1
    with Array#length? or do you mean the object? Commented Mar 2, 2016 at 18:17
  • 1
    Were you really not able to look up the Array.prototype.length property? Commented Mar 2, 2016 at 18:19

5 Answers 5

7
[{"this":"that","int":5},{"this":"that","int":5}].length; // 2
Sign up to request clarification or add additional context in comments.

2 Comments

@krmax44 You had a typo there. jsfiddle.net/52gp5wd2/1 And i thought you just want to count how many objects are there other than primitives,arrays etc...
@JamesSnowy sorry, didn't typed it right in fiddle, but in the project, only the answer of RajaprabhuAravindasamy worked for me...
2

Try,

var cnt = 0;
var arr = [5 , 3 , "not an object" , {"this":"that","int":5},{"this":"that","int":5}];

arr.forEach(function(itm){
 if(!itm.__proto__.__proto__){
  cnt++;
 }
});

console.log(cnt + "normal objects are there"); //2

5 Comments

@krmax44 I glad that I understood your requirement. Try to accept it if you feel this answer has helped you. But this is not a compulsion. :)
@RajaprabhuAravindasamy Great solution, glad you understood what the OP was trying to say - no one else did. I have edited the original post (pending approval) to improve clarity to direct people to your answer.
@mhodges Both the sample and text in the question shows nothing of an array with mixed item types. Changing it will invalidate all answers but this, so I changed it back. If that where what the OP was after, a new question needs to be posted.
@LGSon Yeah, I don't think that the true intent was clearly communicated. Considering the fact that a simple .length did not work for the OP, I am guessing that the array is heterogeneous (or at least has the potential to be), which is why this solution is the only solution that worked.
@mhodges Still, one can't alter a posted question so it will invalidate the answers already given.
2
var length = arrayName.length;

Comments

0

Simply like this:

var yourArrayName = arrayName.length 

As when you have an array and use the .length it counts how many objects there are in the array.

Comments

0

Object {} inside array [] treated as items so you find the length of array to count the item(s)

var $data=[{"this":"that","int":5},{"this":"that","int":5}];

    var count=$data.length

3 Comments

$data, just a variable name nothing else
i am using this convention while declaring the variable, you can use your own convention
You have a right to use any convention to declare a variable, but if so bear in mind that, in the case of the $ sign, some libraries (e.g. jQuery) may use it too, which could lead to confusion.

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.