有没有办法像这样转换数组:
"team" : [ { "player" : "John", "rank" : 0 }, { "player" : "Peter", "rank" : 2 } ]
为此:
"team" : [ "John", "Peter" ]
使用 Underscore.js 还是 jQuery?
如有任何帮助,我们将不胜感激。
最佳答案
使用pluck
:
A convenient version of what is perhaps the most common 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"]
关于javascript - 使用 Underscore.js 或 jQuery - JavaScript 按字段将对象数组转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26765386/