0

I have the following JSON structure. I am trying to use this object for calculating the number of dimensions (which in this case is 4). The number of levels in each row?

How can I achieve that?

 {
  "row": {
    "0": {
      "dimension": "Marketing",
      "level1": "testlevel1",
      "level2": "testlevel2",
      "level3": "testlevel3"
    },
    "1": {
      "dimension": "Sales",
      "level1": "Northeast",
      "level2": "NorthWest",
      "level3": "WestCoast"
    },
    "2": {
      "dimension": "Distribution",
      "level1": "Northeast",
      "level2": "NorthWest",
      "level3": "WestCoast"
    },
    "3": {
      "dimension": "Campaign",
      "level1": "Northeast",
      "level2": "NorthWest",
      "level3": "WestCoast"
    }
  }
}

When I tried

for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    console.log(obj[key]);
  }
}

It just prints a row.

Zakaria Acharki
  • 63,488
  • 15
  • 64
  • 88
Azim Shaik
  • 151
  • 4
  • 14
  • @ArupRakshit yeah its true. that is four – Azim Shaik Aug 22 '18 at 18:11
  • 1
    use `Object.keys(obj)` to get the array of object keys and use `.length` – StefansArya Aug 22 '18 at 18:11
  • Ok. what you want to get out of it? – Arup Rakshit Aug 22 '18 at 18:11
  • Will there ever be properties on the nested object that aren't "levels" or "dimension"? It would be very helpful if you edited your question to include an expected output. – Tyler Roper Aug 22 '18 at 18:12
  • 1
    obj only has 1 field, which is "row", you need to get Object.keys(obj["row"]) – Ji_in_coding Aug 22 '18 at 18:12
  • 2
    Side Note; it's a slight code smell to have an object with keys of 0-n, where an array would serve the same purpose without additional json markup. – Taplar Aug 22 '18 at 18:13
  • 1
    Possible duplicate of [How to efficiently count the number of keys/properties of an object in JavaScript?](https://stackoverflow.com/questions/126100/how-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-javascrip) – Calvin Nunes Aug 22 '18 at 18:14
  • @ArupRakshit I wanted to count number of rows and number of levels in each row. – Azim Shaik Aug 22 '18 at 18:17
  • Do you have any control over how the JSON gets created? Because it would make much more sense if the rows were arrays: `{ rows: [{"dimension":"Marketing", "test":"test"}, {"dimension":"Sales", "test":"test"}]}`. In other words, the `"0" : ` keys are a clumsy way to organise the objects. – Kokodoko Aug 22 '18 at 18:25
  • @Kokodoko I do have control over JSON. This is helpful – Azim Shaik Aug 22 '18 at 19:13

3 Answers3

2

You can do it like this if all you want is to count them.

 var count = 0;
for (var key in obj.row) {
  if (key.dimension) {
   count++;
  }
}
console.log(count);
Rudy Lister
  • 176
  • 8
0
let obj = /..Json../
for(let elem of obj.row) {
  let count = 0;
  for(let key in elem) {
    if(key.indexOf('level') != -1) {
      count++
    }
  }
  console.log(count)
}

As long as you have nested objects, you should first go inside 'row', and then loop through it's keys and count your levels.

Daniyal Lukmanov
  • 969
  • 6
  • 19
0

Try this :

var obj =  {
  "row": {
    "0": {
      "dimension": "Marketing",
      "level1": "testlevel1",
      "level2": "testlevel2",
      "level3": "testlevel3"
    },
    "1": {
      "dimension": "Sales",
      "level1": "Northeast",
      "level2": "NorthWest",
      "level3": "WestCoast"
    },
    "2": {
      "dimension": "Distribution",
      "level1": "Northeast",
      "level2": "NorthWest",
      "level3": "WestCoast"
    },
    "3": {
      "dimension": "Campaign",
      "level1": "Northeast",
      "level2": "NorthWest",
      "level3": "WestCoast"
    }
  }
};

var count = 0;
for (var i in Object.keys(obj.row)) {
  if (obj.row[Object.keys(obj.row)[i]].hasOwnProperty('dimension')) {
    count++;
  }
}

console.log("Count", count);
Rohit Jindal
  • 11,704
  • 11
  • 56
  • 92