我想从array.Some返回真值而不是"true",
我正在使用
var category;
arduair.aqi_ranges[pollutant].some((item,index)=> {
var min =item.range[0];
var max =item.range[1];
if (_.inRange(c,min,max)){
category = index;
return true;
}
return false;
});但这是一个非常丑陋的表达。
发布于 2016-11-25 09:17:55
看起来你的输入数据是对象
变体1.使用lodash pickBy
_.chain(arduair.aqi_ranges[pollutant])
.pickBy(function(item, cat) {
return _.inRange(
c,
item.range[0],
item.range[1]
)
})
.keys()
.first() //remove this step if you want all mathed category
.value();变体2.使用lodash reduce和find (更可靠的变体)
_.chain(arduair.aqi_ranges[pollutant])
.reduce(function(result, val, key) {
return _.concat(
result,
_.merge(val, {category: key})
);
}, [])
.find(function(item) { // use filter if you want all mathed category
return _.inRange(
c,
item.range[0],
item.range[1]
);
})
.get('category')
.value();https://stackoverflow.com/questions/40794044
复制相似问题