javascript - Return all the values of an array in json -
javascript - Return all the values of an array in json -
i have json posts blog , wanna homecoming values of "tags" array. so, see json illustration below:
"posts":[ { "id":"89319077059", "title":"post title 01", "tags":[ "politcs", "science", ] }, { "id":"89318918989", "title":"post title 02", "tags":[ "football", "soccer", ] }, ]
so, need tags values in loop, example:
for (var = 0; < posts.length; i++) { console.info("here [i = 0] should show politcs , science , after [i = 1] show football game , soccer"); }
i tried create other loop search tags , using tags[1], tags[2], etc don't works.
var tags = []; (var tag in posts[i].tags) { tags.push(tag); }
any idea?
if understand well, can use
loop:
var tags = []; (var = 0; < posts.length; ++i) { tags.push(posts[i].tags); }
es5 map
:
var tags = posts.map(function(post){ homecoming post.tags; });
es5 map
+ es6 arrow functions:
var tags = posts.map(post => post.tag);
javascript json es5-shim
Comments
Post a Comment