我想使用pluck函数从项列表中提取值。在chrome javascript控制台中,我的列表如下所示:
[Object, Object, Object, Object, Object]
0: Object
$$hashKey: "004"
_id: "531e2252b0895bcfb8114e8b"
country: "United States"
__proto__: Object
1: Object
2: Object
3: Object
4: Object
length: 5
__proto__: Array[0]
我尝试使用_ .pluck(myArray, country)
来提取国家/地区列表但不起作用。
如何使用pluck获取['United States', ....]
?
非常感谢
答案 0 :(得分:1)
您必须将属性名称用作字符串,例如
_.pluck(myArray, "country");
例如,
var myArray = [{"country": "United States"}, {"country": "United Kingdom"},
{"country": "India"}];
console.log(_.pluck(myArray, "country"));
# [ 'United States', 'United Kingdom', 'India' ]