0
[null, {
  "display_with": "7",
  "id": "1",
  "image": "/images/salt_sugar.png",
  "name": "Salt and Sugar",
  "subcategories": {
    "1": true,
    "6": true,
    "7": true
  }
}, {
  "display_with": "6",
  "id": "2",
  "image": "/images/tea_and_coffee.png",
  "name": "Tea and Coffee",
  "subcategories": {
    "8": true,
    "9": true,
    "124": true
  }
}]

In the above string i want 1, 6, 7 and 8, 9, 124 from second and third record respectively.

This is my logic.

recvCategories = JSON STRING

for (var j=0; j<recvCategories.length; ++j){
    var category =  recvCategories[j];

    if (category != undefined){
        var subcategories = [];
        int size = Object.keys(category.subcategories).length;

        for (var property in object) {
            if (object.hasOwnProperty(property)) {
                // do stuff
            }
        }
    }
}

How to print 1, 6, 7 and 8, 9, 124 in // do stuff ??

Srujan Reddy
  • 730
  • 1
  • 6
  • 27
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131
  • [`Object.keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) – str Aug 15 '16 at 05:38
  • You can do `subcategories.push()` and later you can print it. Also note, `object.keys` should be sufficient. And `Object.keys(category.subcategories).length;` this can break your code if object does not has property subcategories – Rajesh Aug 15 '16 at 05:39
  • 1
    Possible duplicate of [Getting JavaScript object key list](http://stackoverflow.com/questions/3068534/getting-javascript-object-key-list) – Nico Aug 15 '16 at 05:41

3 Answers3

2

Assuming your data is named data this should do it.

var keys = [];
data.forEach(d => {
    if (d.subcategories)
        for (var key in d.subcategories)
            keys.push(key);
})

It may look simple however by using a for(var x in y) will actually iterate the properties of an object and return the propertyNames.

So in the example we call the .forEach() method in an array and then iterate each key of subcategories pushing them into a new array.

Nico
  • 12,493
  • 5
  • 42
  • 62
1

Something like,

for( i in aList) { 
    console.log(keys(aList[i] && aList[i].subcategories)) 
}
// []
// [1, 6, 7]
// [8, 9, 124]
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
0

Few pointers in your code:

  • This condition: if (category != undefined) will not validate null and code will throw on object.something
  • Object.keys(category.subcategories).length; will break if you do not have property subcategories in object. You can try something like this (Object.keys(category.subcategories) || []).length
  • Also if you create your own custom object, then it makes sense to use object.hasOwnProperty, but if you are reading form JSON, you can rely on Object.keys

You can also try something like this:

function getSubCategoriesKeys(d){
  return d.reduce(function(p,c){
    if(!isEmpty(c) && typeof(c) === "object" && c.hasOwnProperty("subcategories")){
      p = p.concat(Object.keys(c.subcategories))
    }
    return p;
  }, [])
}

function isEmpty(o){
  return o === undefined || o === null || o.toString().trim().length === 0
}

var data = [null, {
  "display_with": "7",
  "id": "1",
  "image": "/images/salt_sugar.png",
  "name": "Salt and Sugar",
  "subcategories": {
    "1": true,
    "6": true,
    "7": true
  }
}, {
  "display_with": "6",
  "id": "2",
  "image": "/images/tea_and_coffee.png",
  "name": "Tea and Coffee",
  "subcategories": {
    "8": true,
    "9": true,
    "124": true
  }
}]

var keys = getSubCategoriesKeys(data);
console.log(keys)

Reference

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79