Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count a JavaScript object's attributes? [duplicate]

Suppose I have the following object in JavaScript:

var object = {   "key1": "value1",   "key2": "value2",   "key3": "value3" };  

How do I find out how many values exist in the object?

like image 992
Eugeniu Torica Avatar asked Aug 28 '09 09:08

Eugeniu Torica


People also ask

How do you check if an array of objects has duplicate values in JavaScript?

Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate. All such elements are returned in a separate array using the filter() method.

How do you count duplicate values in an array?

To count the duplicates in an array:Copied! const arr = ['one', 'two', 'one', 'one', 'two', 'three']; const count = {}; arr. forEach(element => { count[element] = (count[element] || 0) + 1; }); // 👇️ {one: 3, two: 2, three: 1} console.


1 Answers

You can do that by using this simple code:

Object.keys(myObject).length 
like image 125
deadrunk Avatar answered Sep 23 '22 02:09

deadrunk