我有这个对象
{'a': 'test1', 'b': 'test2', 'c': 'test3'}
然后我
[{name: 'test1', selected: true}, {name: 'test3', selected: true}]
现在我想获得在第二个对象中选中的键的最终列表
['a', 'c']
我如何用下划线
来做到这一点我试过这个
var inv_obj = _.invert(obj);
return _
.filter(objectList, function (obj) {
return obj.selected
})
.map(function (obj, inv_obj) {
return inv_obj[obj] ?:''
})
答案 0 :(得分:1)
尝试:
var a={'a': 'test1', 'b': 'test2', 'c': 'test3'};
var b=[{name: 'test1', selected: true}, {name: 'test3', selected: true}];
var c=_.pluck(_.where(b, {selected: true}), 'name');
// ['test1', 'test3']
var d=_.invert(a);
console.log(_.values(_.pick(d, c)));
答案 1 :(得分:0)
var obj = {'a': 'test1', 'b': 'test2', 'c': 'test3'};
var col = [{name: 'test1', selected: true}, {name: 'test2', selected: false}, {name: 'test3', selected: true}];
var inv_obj = _.invert(obj);
var final = _.filter(col, function(obj) {
return obj.selected;
}).map(function (obj) {
return inv_obj[obj.name];
});
console.log(final);