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 答案 ),并将文本转换为小写:)
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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…