我试图循环遍历具有不同键的对象数组。有没有办法可以根据键来选择对象?
var array = [
{
"1400": "Accident and Health"
},
{
"100": "Life"
},
{
"1300": "Pension"
}
]
var a = "100";
var pop = _.pick(array,a);
console.log(pop);
期望的输出:
生命
谢谢!
答案 0 :(得分:3)
您可以使用var array = [{ 1400: "Accident and Health" }, { 100: "Life" }, { 1300: "Pension" }];
var result = (key => array.find(item => key in item)[key])(100);
console.log(result);
运算符。
{{1}}
答案 1 :(得分:0)
var array = [
{
"1400": "Accident and Health"
},
{
"100": "Life"
},
{
"1300": "Pension"
}
]
var a = "100";
var pop = _.map(array, function(currentObject) {
var b= _.pick(currentObject, a);
if(!_.isEmpty(b))
console.log(b);
});

<script src="http://underscorejs.org/underscore-min.js"></script>
&#13;
答案 2 :(得分:0)
Lodash 4.17.2
_.chain(array).map('100').compact().head().value();