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 ??

3
  • Object.keys() Commented Aug 15, 2016 at 5: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 Commented Aug 15, 2016 at 5:39
  • 1
    Possible duplicate of Getting JavaScript object key list Commented Aug 15, 2016 at 5:41

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Something like,

for( i in aList) { 
    console.log(keys(aList[i] && aList[i].subcategories)) 
}
// []
// [1, 6, 7]
// [8, 9, 124]

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.