-5
"namelist":{
            "name":"xyz",
            "version":"1.0.0"
         }

How to find the length of the two values inside the namelist?

Jim O'Brien
  • 2,512
  • 18
  • 29
sam
  • 67
  • 1
  • 1
  • 7

2 Answers2

6

You can find size of object (i.e total number of attributes in object)like this:

namelist = { "name":"xyz", "version":"1.0.0" }
var size = Object.keys(namelist).length;
console.log(size);

Output: 2

For getting size of value of name attribute ( e.g size of "xyz" in your case)

console.log(namelist.name.length)

Output: 3

For getting size of value of version attribute( e.g size of "1.0.0" in your case)

console.log(namelist.version.length)

Output: 5

Al Fahad
  • 2,378
  • 5
  • 28
  • 37
1

Get the keys as an array using Object.keys, and take the length of that array:

const obj = {
  namespace: {
    key1: 'whatever',
    key2: 'whatever2',
  }
}

const keys = Object.keys(obj.namespace) // ['key1', 'key2']
const keysLength = keys.length // 2
mndewitt
  • 66
  • 3