1

I am trying to get a list of tags from an object

Here is the full code and data:

$(document).ready(function() {

obj = 

{
    "category":[
    {

    "id":"8098",
        "tags":{
            "411":"something",
            "414":"something else"
        }
    }

]

};


var tagList = [];

for (var index in obj) {

  for (var indexSub in obj[index].tags) {

       blah = (obj[index].tags[indexSub]);
       tagList.push(blah);
   }
}

console.log(tagList);



});

Here's a link to jsFiddle

The problem is that taglist is returning an empty array.

How can I fix this?

  • [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Andreas Oct 19 '16 at 14:12
  • "id" is not in the same level as "category" its actually inside the array that "category" refers to, so your iteration never finds the "id" key – StackOverMySoul Oct 19 '16 at 14:13
  • I've added a link to jsfiddle –  Oct 19 '16 at 14:14
  • Are all of your objects going to have the same structure as this one? If so, you can just loop through obj.category.id.tags, otherwise we'll have to fix the loops you have now. I'll post an answer based on your reply to this. – Brandon Oct 19 '16 at 14:19
  • Yes, I just want to get the list of tag names and put them into an array –  Oct 19 '16 at 14:20

4 Answers4

0

Yours

    for (var index in obj) {

        for (var indexSub in obj[index].tags) {

            blah = (obj[index].tags[indexSub]);
            tagList.push(blah);
        }
    }

New to get the tags that are into the category

for (var index in obj) {
  for (var indexSub in obj[index]) {
     blah = (obj[index][indexSub].tags);
     tagList.push(blah);
  }
} 

See the diference in the way to access the properties

 
        obj =
            {
                "category": [
                    {
                        "id": "8098",
                        "tags": {
                            "411": "something",
                            "414": "something else"
                        }
                    }

                ]

            };


        var tagList = [];

        for (var index in obj) {

            for (var indexSub in obj[index]) {
                tagList = (obj[index][indexSub].tags);
               // tagList.push(blah);
            }
        }

        console.log(tagList);
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
0

You need to iterate the category, and then get the keys and values of tags, somethig like this..

var obj = 
{
    "category":[
    {

    "id":"8098",
        "tags":{
            "411":"something",
            "414":"something else"
        }
    }

]};

var tagList = [];

obj.category.forEach( o =>{
  for (property in o.tags){
   tagList.push(property + ':'+ o.tags[property]);  
  };
});

console.log(tagList);
BrTkCa
  • 4,703
  • 3
  • 24
  • 45
0

Here is your jsfiddle

obj = 
{
   "category":[
          {
             "id":"8098",
             "tags":{
                 "411":"something",
                 "414":"something else"
             }
          }

   ]
};
var tagList = [];
for (key in obj) {
    for(subKey in obj[key]){
            var myTags=obj[key][subKey].tags;
            for(tag in myTags){
                 tagList.push(tag);
            }
    }

}
console.log(tagList);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

You could use Array#reduce and Object.keys with Array#map for iterating and getting all tags.

var obj = { "category": [{ "id": "8098", "tags": { "411": "something", "414": "something else" } }] },
    tagList = obj.category.reduce(function (r, a) {
        return r.concat(Object.keys(a.tags).map(function (t) { return a.tags[t]; }));
    }, []);

console.log(tagList);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392