javascript 获取过滤值的对象键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16987705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get object keys for filtered values
提问by ducin
The case is simple - I've got a following object:
案例很简单 - 我有以下对象:
Object {1: false, 2: true, 3: false, 4: false, 5: false, 6: false, 7: false, 8: true, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false}
and I need to get an array of ids that had true value, using underscore. In above case that would be:
并且我需要使用下划线获取具有真实值的 id 数组。在上述情况下,将是:
[2, 8]
I tried few things but I'm a bit stuck. Does anyone have any idea?
我尝试了几件事,但我有点卡住了。有谁有想法吗?
回答by gustavohenke
var keys = [];
_.each( obj, function( val, key ) {
if ( val ) {
keys.push(key);
}
});
There may be easier/shorter ways using plain Underscore.
使用普通的 Underscore 可能有更简单/更短的方法。
In case anyone here uses Lodashinstead of Underscore, the following is also possible, which is very shortand easy to read:
如果这里有人使用Lodash而不是Underscore,以下也是可能的,它非常简短且易于阅读:
var keys = _.invert(obj, true)[ "true" ];
回答by Cláudio Silva
Giorgi Kandelaki gave a good answer, but it had some potential problems (see my comment on his answer).
Giorgi Kandelaki 给出了一个很好的答案,但它有一些潜在的问题(见我对他的回答的评论)。
The correct way is:
正确的方法是:
_(obj).pairs().filter(_.last).map(_.first)
or
或者
_.map(_.filter(_.pairs(obj),_.last),_.first)
回答by Dmitri Algazin
var data = {1: false, 2: true, 3: false, 4: true};
var filteredIds = _.filter(_.keys(data), function (key) {
return data[key];
});
// result [2, 4]
var rejectedIds = _.reject(_.keys(data), function (key) {
return data[key];
});
// result [1, 3]
回答by P?l
You can use _.pick
. Like this:
您可以使用_.pick
. 像这样:
var data = {1: false, 2: true, 3: false, 4: false, 5: false, 6: false, 7: false, 8: true, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false}
var keys = _.keys(_.pick(data, function(value) {
return value;
}));
回答by Voxel
var obj = {1: false, 2: true /*...*/};
you can use reduce:
你可以使用减少:
_(obj).reduce(function(memo, val, key){
if (val)
memo.push(key);
return memo;
}, []);
or chain map:
或链图:
_(obj).chain().map(function(val, key){
if (val)
return key;
}).reject(_.isUndefined).value();
回答by Giorgi Kandelaki
You can try this:
你可以试试这个:
_.pairs(obj)
.filter(function(pair) { return pair[1] })
.map(function(pair) { return pair[0] })
Or the same but bit more concise:
或相同但更简洁:
_.pairs(obj).filter(_.last).map(_.first)
回答by gentiane
You can do it very easily
你可以很容易地做到
var obj = { a: false, b: false, c: true, d: false, e: true, f: false };
name, names;
name = _.findKey(obj, true);
// -> 'c'
names = _.findKeys(obj, true);
// -> ['c', 'e']
For that you juste need to extend underscore a little :
为此,您需要稍微扩展下划线:
_.mixin({
findKey: function(obj, search, context) {
var result,
isFunction = _.isFunction(search);
_.any(obj, function (value, key) {
var match = isFunction ? search.call(context, value, key, obj) : (value === search);
if (match) {
result = key;
return true;
}
});
return result;
},
findKeys: function(obj, search, context) {
var result = [],
isFunction = _.isFunction(search);
_.each(obj, function (value, key) {
var match = isFunction ? search.call(context, value, key, obj) : (value === search);
if (match) {
result.push(key);
}
});
return result;
}
});
And you can even use a function as filter, like this :
您甚至可以使用函数作为过滤器,如下所示:
var team = {
place1: { name: 'john', age: 15 },
place2: { name: 'tim', age: 21 },
place3: { name: 'jamie', age: 31 },
place4: { name: 'dave', age: 17 }}
// Determine the places of players who are major
var placeNames = _.findKeys(team, function(value) { return value.age >= 18; });
// -> ['place2', 'place3']
Enjoy ;-)
享受 ;-)
回答by sprynk
My version:
我的版本:
var list = {
1: false, 2: true, 3: false, 4: false,
5: false, 6: false, 7: false, 8: true,
12: false, 13: false, 14: false, 15: false,
16: false, 17: false, 18: false, 19: false
};
_.chain(list).map(function(val, key) {
return val ? parseInt(key) : undefined
}).reject(function(val) {
return _.isUndefined(val);
}).value();
// returns [2,8]
回答by emil
This might be easier
这可能更容易
result = _.chain(obj)
.map(function(value, key){
return value?key:false;
})
.filter(function(v){
return v
})
.value();
回答by Manu
I use this one:
Retrieve all keys with the same value from an object (not only an object with just boolean values) using lodash
我使用这个:
使用lodash
function allKeys(obj, value) {
_.keys(_.pick(obj, function (v, k) { return v === value; }));
}
Example:
例子:
var o = {a: 3, b: 5, c: 3};
var desiredKeys = allKeys(o, 3);
// => [a, c]