Gaurav Singh Gaurav Singh - 24 days ago 6
JSON Question

How to find out JSON object Size

legend=[{"min":0,
"max":first_color,
"color":"#1a9850"
},
{
"min":first_color,
"max":sec_color,
"color":"#fee08b"
},
{
"min":sec_color,
"max":thrd_color,
"color":"#ff3300"
},
{
"min":thrd_color,
"max":frth_color,
"color":"#d73027"
"Abc":"gsfg"
}

];


I want to find out each object size like first one have size there second one alse have size three but fourth one have size four

Answer

Iterate over the array and get object property names count.

var legend = [{
  "min": 0,
  "max": 'first_color',
  "color": "#1a9850"
}, {
  "min": 'first_color',
  "max": 'sec_color',
  "color": "#fee08b"
}, {
  "min": 'sec_color',
  "max": 'thrd_color',
  "color": "#ff3300"
}, {
  "min": 'thrd_color',
  "max": 'frth_color',
  "color": "#d73027",
  "Abc": "gsfg"
}];

var res = legend.map(function(v) {
  console.log(Object.keys(v).length);
  return Object.keys(v).length;
});

console.log(res);