我如何根据下划线中的值获取键
How can i get keys based on values in underscore
我有这个对象
{'a': 'test1', 'b': 'test2', 'c': 'test3'}
那我有
[{name: 'test1', selected: true}, {name: 'test3', selected: true}]
现在我想获得在第二个对象中选择为 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] ?:''
})
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);
尝试:
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)));
我有这个对象
{'a': 'test1', 'b': 'test2', 'c': 'test3'}
那我有
[{name: 'test1', selected: true}, {name: 'test3', selected: true}]
现在我想获得在第二个对象中选择为 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] ?:''
})
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);
尝试:
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)));