Javascript Lodash _.filter 函数只能满足一个条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31587555/
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
Lodash _.filter function must only meet ONE condition
提问by Marcus Christiansen
I am currently using the Lodash _.filter()function to filter objects which meet a specific condition based on the user search, which could either be a street address, town or country.
我目前正在使用 Lodash _.filter()函数来过滤满足基于用户搜索的特定条件的对象,可以是街道地址、城镇或国家。
I have extracted the search string and want to filter this against several values within each object and must return the object if the string is found in any of those keys.
我已经提取了搜索字符串,并希望针对每个对象中的多个值对其进行过滤,并且如果在这些键中的任何一个中找到该字符串,则必须返回该对象。
To explain I want to use the example from the website.
为了解释,我想使用网站上的例子。
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
];
// using the `_.matches` callback shorthand
_.result(_.find(users, { 'age': 1, 'active': true }), 'user');
In this example, how would I filter for users who either are 36 years of age OR are active?
在此示例中,我将如何过滤 36 岁或活跃的用户?
According to the documentation it seems that both conditions needs to be met, as per the above example for the object to be returned.
根据文档,这两个条件似乎都需要满足,根据上面要返回的对象的示例。
回答by npe
回答by Ivan Pirus
Also works second solution in Vue.js
也适用于 Vue.js 中的第二个解决方案
users.filter(user => {
return user.age == 36 || user.active;
});