最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return all the values of an array in json - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Jun 22, 2014 at 0:00 Tiago Barreto asked Jun 21, 2014 at 18:51 Tiago BarretoTiago Barreto 82213 silver badges31 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 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);
    

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

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);

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.

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.

发布评论

评论列表(0)

  1. 暂无评论