3

I have a json with all posts my blog and I wanna to return all the values of "tags" array. So, see the json example below:

"posts":[
  {
     "id":"89319077059",
     "title":"Post Title 01",
     "tags":[
        "Politcs",
        "Science",
     ]
  },
  {
     "id":"89318918989",
     "title":"Post Title 02",
     "tags":[
        "Football",
        "Soccer",
     ]
  },
]

So, I need to get the only tags values in loop, for example:

for (var i = 0; i < posts.length; i++) {
    console.info("Here [i = 0] should be show the Politcs and Science and after [i = 1] show Football and Soccer");
}

I tried to create a other loop to search tags and using tags[1], tags[2], etc but don't works.

var tags = [];

for (var tag in posts[i].tags) {
     tags.push(tag);
}

Any idea?

Tiago Barreto
  • 822
  • 13
  • 31

5 Answers5

4

If I understand it well, you can use

  • Loop:

    var tags = [];
    for (var i = 0; i < posts.length; ++i) {
         tags.push(posts[i].tags);
    }
    
  • ES5 map:

    var tags = posts.map(function(post){
        return post.tags;
    });
    
  • ES5 map + ES6 arrow functions:

    var tags = posts.map(post => post.tag);
    
Oriol
  • 274,082
  • 63
  • 437
  • 513
1

Here is code :

var posts  = [
  {
     "id":"89319077059",
     "title":"Post Title 01",
     "tags":[
        "Politcs",
        "Science",
     ]
  },
  {
     "id":"89318918989",
     "title":"Post Title 02",
     "tags":[
        "Football",
        "Soccer",
     ]
  }
];

var tags = [];

for (var index in posts) {
    var tagsArray = posts[index].tags;
     tags.push(tagsArray);
}
console.log(tags);

Jsbin

OzerGul
  • 19
  • 10
  • Note `for...in` loops are considered a bad way to iterate arrays. They will iterate all enumerable properties, including non numerical ones and those coming from prototype. And won't iterate integers between 0 and lenght-1 if there's no property, e.g. `[0,,,,,5]` – Oriol Jun 21 '14 at 19:00
  • I didn't know it! Why, can you explain? – OzerGul Jun 21 '14 at 19:01
  • 1
    Thanks @OzerGul but I will to use maps, Oriol tips. – Tiago Barreto Jun 21 '14 at 19:06
1

In your for (var tag in posts[i].tags) loop, tag contains the keys, not the values.

So a quick fix would be:

for (var tag in posts[i].tags) {
    tags.push(posts[i].tags[tag]);
}

But as Oriol pointed out, for..in loops are meant to iterate through object properties, not arrays.

Just use a for loop like your first one.

Gras Double
  • 15,901
  • 8
  • 56
  • 54
1

Try this,

var posts = {"posts":[
  {
     "id":"89319077059",
     "title":"Post Title 01",
     "tags":[
        "Politcs",
        "Science",
     ]
  },
  {
     "id":"89318918989",
     "title":"Post Title 02",
     "tags":[
        "Football",
        "Soccer",
     ]
  },
]};


var tags = [];

for(var i=0;i<posts.posts.length;i++) {
    for(var j=0;j<posts.posts[i].tags.length;j++) {
        tags.push(posts.posts[i].tags[j]);
    }
}

console.log(tags);
Rashmin Javiya
  • 5,173
  • 3
  • 27
  • 49
1

You can try to use a for loop:

for (var i = 0; i < posts.length; i++) {
    for(var j = 0; j < posts[i].tags.length; j++) {
        console.log(posts[i].tags[j]);
    }
}

Cheers.

TonyMtz
  • 46
  • 3