admin管理员组文章数量:1425696
Is there a way to convert an array like this:
"team" : [ { "player" : "John", "rank" : 0 }, { "player" : "Peter", "rank" : 2 } ]
to this:
"team" : [ "John", "Peter" ]
with Underscore.js or jQuery?
Any help would be greatly appreciated.
Is there a way to convert an array like this:
"team" : [ { "player" : "John", "rank" : 0 }, { "player" : "Peter", "rank" : 2 } ]
to this:
"team" : [ "John", "Peter" ]
with Underscore.js or jQuery?
Any help would be greatly appreciated.
Share Improve this question asked Nov 5, 2014 at 19:23 user3475602user3475602 1,2172 gold badges22 silver badges43 bronze badges3 Answers
Reset to default 7Use pluck
:
A convenient version of what is perhaps the most mon use-case for map: extracting a list of property values.
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.pluck(stooges, 'name');
=> ["moe", "larry", "curly"]
You could do it with plain old javascript via the map function
var someArray = [
{name:'John', age:23},
{name:'Foo', age:34},
{name:'Bar', age:10},
{name:'Doe', age:65}
];
var nameArray = someArray.map(function(el) {return el.name;});
With pure JavaScript Just iterate through it and push the player into a new array:
var array = [];
for(i in team){
array.push(team[i].player);
}
or with underscore.js use plug
var array = _.pluck(team, 'player');
本文标签: Convert array of objects into array by field with Underscorejs or jQueryJavaScriptStack Overflow
版权声明:本文标题:Convert array of objects into array by field with Underscore.js or jQuery - JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745456917a2659145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论