Javascript 如何对对象使用 Underscore.js 过滤器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11697702/
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
How to use Underscore.js filter with an object?
提问by AnApprentice
I have an object like so:
我有一个像这样的对象:
> Object
> [email protected]: Array[100]
> [email protected]: Array[4]
> 0
id : 132
selected : true
> 1
id : 51
selected : false
etc..
等等..
How can I use the underscore _.filter()
to return back only the items where selected === true?
如何使用下划线_.filter()
仅返回选择 === true 的项目?
I've never had the need to go down to layers with _.filter()
. Something like
我从来没有必要使用_.filter()
. 就像是
var stuff = _.filter(me.collections, function(item) {
return item[0].selected === true;
});
Thank you
谢谢
采纳答案by rjz
If you want to pull all array elements from any e-mail address where selected
is true
, you can iterate like so:
如果您想从selected
is 的任何电子邮件地址中提取所有数组元素true
,您可以像这样迭代:
var selected = [];
for (email in emailLists) {
selected.concat(_.filter(emailLists[email], function (item) {
return item.selected === true;
}));
}
If you only want to pull the arrays where allelements are selected
, you might instead do something like this:
如果您只想拉出所有元素所在的数组selected
,您可以改为执行以下操作:
var stuff = _.filter(me.collections, function(item) {
return _.all(item, function (jtem) {
jtem.selected === true;
});
});
回答by Pathogen
Underscore's filter method will work on an object being used as a hash or dictionary, but it will return an array of the object's enumerable values and strip out the keys. I needed a function to filter a hash by its values that would preserve the keys, and wrote this in Coffeescript:
Underscore 的 filter 方法适用于用作散列或字典的对象,但它会返回对象可枚举值的数组并去除键。我需要一个函数来过滤散列值以保留键值,并在 Coffeescript 中这样写:
hash_filter: (hash, test_function) ->
keys = Object.keys hash
filtered = {}
for key in keys
filtered[key] = hash[key] if test_function hash[key]
filtered
If you're not using Coffeescript, here's the compiled result in Javascript, cleaned up a little:
如果你没有使用 Coffeescript,这里是用 Javascript 编译的结果,稍微清理了一下:
hash_filter = function(hash, test_function) {
var filtered, key, keys, i;
keys = Object.keys(hash);
filtered = {};
for (i = 0; i < keys.length; i++) {
key = keys[i];
if (test_function(hash[key])) {
filtered[key] = hash[key];
}
}
return filtered;
}
hash = {a: 1, b: 2, c: 3};
console.log((hash_filter(hash, function(item){return item > 1;})));
// Object {b=2, c=3}
TL; DR: Object.keys()is great!
TL; DR:Object.keys()很棒!
回答by Dexygen
I have an object called allFilterValues containing the following:
我有一个名为 allFilterValues 的对象,其中包含以下内容:
{"originDivision":"GFC","originSubdivision":"","destinationDivision":"","destinationSubdivision":""}
This is ugly but you asked for an underscorebased way to filter an object. This is how I returned only the filter elements that had non-falsy values; you can switch the return statement of the filter to whatever you need:
这很丑陋,但您要求使用基于下划线的方式来过滤对象。这就是我只返回具有非虚假值的过滤器元素的方式;您可以将过滤器的 return 语句切换为您需要的任何内容:
var nonEmptyFilters = _.pick.apply({}, [allFilterValues].concat(_.filter(_.keys(allFilterValues), function(key) {
return allFilterValues[key];
})));
Output (JSON/stringified):
输出(JSON/字符串化):
{"originDivision":"GFC"}
回答by Jorge Alberto
Maybe you want a simplest way
也许你想要一个最简单的方法
_.filter(me.collections, { selected: true})