如何使用 underscorejs 从数组中选择多个属性?

How to pick multiple properties from an array using underscorejs?

如何仅使用 underscorejs 从数组中选取多个属性?

var arr = [{Analytics:16}, {Technology:12}, {Medical:20}, {Operations:40}]

我需要以下格式的输出。 [12,40,20,16]

谢谢

var arr = [{Analytics:16}, {Technology:12}, {Medical:20}, {Operations:40}];
var output = [];

_.each(arr, function (e){output.push(_.flatten(e)[0])});

output = [16, 12, 20, 40];
var arr = [{Analytics:16}, {Technology:12}, {Medical:20}, {Operations:40}];
_.map(arr,function(m){return _.values(m)[0]});

您可以先 map over the array and get the values and then flatten 结果:

var result = _.flatten(_.map(arr, _.values));