javascript - Using Lodash to find a partial string in any property value of an object - Stack Overflow

admin2025-04-04  0

I want to use Lodash to return true when an object contains in any of its values, a match for a partial string. I've tried this with _.includes as follows.

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. I want result to return true. How can I do this in Lodash?

I want to use Lodash to return true when an object contains in any of its values, a match for a partial string. I've tried this with _.includes as follows.

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. I want result to return true. How can I do this in Lodash?

Share Improve this question edited Nov 29, 2019 at 5:51 Let Me Tink About It asked May 29, 2019 at 14:46 Let Me Tink About ItLet Me Tink About It 16.2k21 gold badges108 silver badges217 bronze badges 5
  • Lodash is overkill for a simple problem. – Kobe Commented May 29, 2019 at 14:48
  • 1 You can do result = Object.values(ob).some(str => _.includes(str, search)); – user5734311 Commented May 29, 2019 at 14:50
  • @ChrisG my thought exactly – Kobe Commented May 29, 2019 at 14:50
  • 1 @Kobe: You said Lodash is overkill. But you like the answer using Lodash?
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743709806a216072.html

最新回复(0)