1

In the below method,

self.search = (obj, search) => {
...

the second argument, search is an array of search strings. The first is an object.

The algorithm is to be used in a filter method that returns true if an object contains any of the search strings.

self.search = (obj, search) => {
  return Object.keys(obj).some(k =>
    typeof obj[k] === 'object'
      ? self.search(obj[k], search)
      : search.some(v => String(obj[k]).includes(v))
  )
}

So, for example, given the following object:

{
  name: 'Peter',
  phone: {
    primary: 5556667777,
    mobile: 1112223333
  }
}

self.search would return true for the following values of the search argument:

  • ['Pet']
  • ['Pet', '111']
  • ['Pet', 'asdf']

The Question

How can self.search be changed to instead return true only if each element of the search array is contained by the object?

Essentially, ['Pet', 'asdf'] should return false.

Update

Replacing some with every does not work when the array of search strings is across levels.

// `['Pet']` correctly returns true
// `['Pet', '111']` returns false when it should return true
// `['Pet', 'asdf']` correctly returns false
self.search = (obj, search) => {
  return Object.keys(obj).some(k =>
    typeof obj[k] === 'object'
      ? self.search(obj[k], search)
      : search.every(v => String(obj[k]).toLowerCase().includes(v))
  )
}
4
  • Have you tried every() instead of some()? Commented Mar 6, 2017 at 13:10
  • @KursadGulseven, Yes, see the update. Commented Mar 6, 2017 at 13:11
  • The values being searched must be a value of an object property or it can be a key too? Commented Mar 6, 2017 at 13:18
  • @AramilRey, it should only be a value of the object. Commented Mar 6, 2017 at 13:20

1 Answer 1

3

You could use your single search function and a meta seach for every single item of the array.

var search = (obj, v) => Object.keys(obj).some(k => typeof obj[k] === 'object' ? search(obj[k], v) : obj[k].toString().includes(v)),
    findAll = (obj, array) => array.every(a => search(obj, a)),
    obj = { name: 'Peter', phone: { primary: 5556667777, mobile: 1112223333 } };

console.log(findAll(obj, ['Pet', '99']));   // false
console.log(findAll(obj, ['Pet']));         // true
console.log(findAll(obj, ['Pet', '111']));  // true
console.log(findAll(obj, ['Pet', 'asdf'])); // false

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.