简体   繁体   English

使用 Lodash 在对象的任何属性值中查找部分字符串

[英]Using Lodash to find a partial string in any property value of an object

I want to use Lodash to return true when an object contains in any of its values, a match for a partial string.当对象包含其任何值时,我想使用 Lodash 返回true ,匹配部分字符串。 I've tried this with _.includes as follows.我已经用_.includes试过了,如下所示。

const ob = {first: "Fred", last: "Flintstone",}
const search = "stone";
const result = _.includes(ob, search)
console.log(result); // false

I've also tried this using a regular expression instead of a string for the search term.我也尝试过使用正则表达式而不是搜索词的字符串。

const search = /stone/gi;

Both times result returns false .两次result返回false I want result to return true .我希望result返回true How can I do this in Lodash?我怎样才能在 Lodash 中做到这一点?

You can use lodash's _.some() (which works with objects), and lodash/vanilla includes to find if the current property's value has the search string:您可以使用 lodash 的_.some() (它适用于对象)和 lodash/vanilla 包含来查找当前属性的值是否具有搜索字符串:

 const includesValue = (val, obj) => _.some(obj, v => _.includes(v, val)) const obj = {first: "Fred", last: "Flintstone",} const search = "stone"; const result = includesValue(search, obj) console.log(result); // true
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

And a lodash/fp version:还有一个 lodash/fp 版本:

 const includesValue = val => _.some(_.includes(val)) const obj = {first: "Fred", last: "Flintstone",} const search = "stone"; const result = includesValue(search)(obj) console.log(result); // true
 <script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

To handle case sensitivity and letters with diacritics, you can use _.deburr() (or the this answer ), and convert the text to lower case:要处理区分大小写和带有变音符号的字母,您可以使用_.deburr() (或 this answer ),并将文本转换为小写:

 const normalize = str => _.toLower(_.deburr(str)) const includesValue = (val, obj) => { const search = normalize(val) return _.some(obj, v => normalize(v).includes(search)) } const obj = {first: "Fred", last: "Flintstoné",} const search = "Stone"; const result = includesValue(search, obj) console.log(result); // true
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

You could find the string partial within an array of the object's values like this.您可以在这样的对象值数组中找到字符串部分。

 const ob = { first: "Fred", last: "Flintstone" } const search = "stone" const isFound = Boolean(Object.values(ob).find(item => item.includes(search))) console.log(isFound)

And as per @Kobe's suggestion, you could use some instead of evaluating the result of find with Boolean() .根据@Kobe 的建议,您可以使用some而不是使用Boolean()评估find的结果。

 const ob = { first: "Fred", last: "Flintstone" } const search = "stone" const isFound = Object.values(ob).some(item => item.includes(search)) console.log(isFound)

That's not how lodash's includes works.这不是 lodash 的包含的工作方式。

You should search the values of the object, not the object itself.您应该搜索对象的值,而不是对象本身。 And you should check each value in turn.您应该依次检查每个值。

Also you don't need lodash for that.你也不需要lodash。

const ob = {first: "Fred", last: "Flintstone",}
const search = "stone";
const result = Object.values(ob).some(value => value.indexOf(search) !== -1);
console.log(result); // true

You could use _filter if you want to use lodash.如果你想使用 lodash,你可以使用 _filter。

const ob = {first: "Fred", last: "Flintstone",}
var filteredOb = _.filter(ob, function(elem) {
    return elem.indexOf("stone") > -1;
});
console.log(filteredOb);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM