Q:

javascript length of object

var size = Object.keys(myObj).length;
15
var person={
    "first_name":"Harry",
    "last_name":"Potter",
    "age":14
};
var personSize = Object.keys(person).length; //gets number of properties (3) 
13
Using the Object.keys() method: The Object.keys() method is used to return the 
object property name as an array. The length property is used to get the number
of keys present in the object. It gives the length of the object.
Syntax:
objectLength = Object.keys(exampleObject).length
0
Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

// Get the size of an object
var size = Object.size(myObj);
3
//length of this object is 4
let object = {
  'A':{
    '10':['a','b','c']
  }, 
  'B':{
    '20':['a','b','c']
  }, 
  'C':{
    '30':['a','b','c']
  }, 
  'D':{
    '40':['a','b','c']
  },   
}

//Get the keys of the object (A,B,C,D) and print how many there are
Object.keys().length
> prints: 4
0

New to Communities?

Join the community