Back
Below is my JavaScript example:
var obj = { key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4'}
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?
See the below code implementation:
var obj = { key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4'};var keys = []; //Passing the objects into an arrayfor (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
};
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.
31k questions
32.8k answers
501 comments
693 users