Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my JavaScript example:

var obj = {

   key1: 'value1',

   key2: 'value2',

   key3: 'value3',

   key4: 'value4'

}

Can anyone tell me how to get the length and list of keys in this JS object? 

1 Answer

0 votes
by (19.7k points)

See the below code implementation: 

var obj = {

  key1: 'value1',

  key2: 'value2',

  key3: 'value3',

  key4: 'value4'

};

var keys = []; //Passing the objects into an array

for (var k in obj) keys.push(k);

console.log("total " + keys.length + " keys: " + keys); //which shows the length of the JS with the objects in it

Interested in Java? Check out this Java Certification by Intellipaat.   

Related questions

0 votes
1 answer
asked Mar 23, 2021 in Web Technology by Jake (7k points)
0 votes
1 answer
0 votes
1 answer
asked Apr 14, 2021 in Java by sheela_singh (9.5k points)

Browse Categories

...