Artemis
Artemis

Reputation: 599

Check if Object Key and value contain certain words

I have a JSON file with Question and Answer pair:

{
  "faq": {
    "How old is John?": "John is 30",
    "What color are Anna's eyes": "They are Blue",
    "What's the name of Anna and John child?": "His name is jack"
  }
}

How can I filter the object for John, Anna and Jack?

Searching for John:

{
  "faq": {
    "How old is John?": "John is 30",
    "What's the name of Anna and John child?": "His name is jack"
  }
}

Searching for Anna should return:

{
  "faq": {
    "What color are Anna's eyes": "They are Blue",
    "What's the name of Anna and John child?": "His name is jack"
  }
}

Searching for Jack:

{
  "faq": {
    "What's the name of Anna and John child?": "His name is jack"
  }
}

is there any way to search for single words in keys and value?

Upvotes: 0

Views: 2175

Answers (3)

giulp
giulp

Reputation: 2408

Check if Object Key and value contain certain words

const that = {
  "faq": {
    "How old is John?": "John is 30",
    "What color are Anna's eyes": "They are Blue",
    "What's the name of Anna and John child?": "His name is jack"
  }
}

const filterKVcontains = (obj, str) => Object.fromEntries(
  Object.entries(obj).filter(
    e => e.every(
      it => (new RegExp(str, 'i')).test(it)
    )
  )
)
    
const filteredFAQ = filterKVcontains(that.faq, 'john')

const demo = document.createElement('pre')
demo.innerHTML = JSON.stringify({'faq':filteredFAQ}, null, 2)
document.body.appendChild(demo)

Let's focus on the variable obj, we give it to

  • Object.entries() that returns an array made from the properties of obj, where each element is itself an array with two entries, one for the key, the other for the value.

We pass this array of k/v entries to

  • Array.prototype.filter() that returns only the elements of the array it operates on, that meet the criterion defined in the function it receives as parameter. We run it on the list of key/value pairs, with the following condition:

    • Array.prototype.every() that returns true if all elements of the array, now our key/value pair, satisfy the condition given as argument through a function:

      • RegExp.prototype.test() wich returns true if the string it is fed matches the regular expression it is run against. Our regular expression is built around str, the parameter to filterKVcontains
  • Object.fromEntries() Step out and rebuild the object from the filtered list of key/value entries


To check if Object key or value contain the words:
substitute Array.prototype.every() with Array.prototype.some() which verifies whether at least one element in the array passes the test implemented by the provided function.

Upvotes: 1

Josh Regev
Josh Regev

Reputation: 379

Here's a simple function .. it checks each pair in the object and if either the key or value matches, it'll get returned in a new object.

const search = (term) => {
    let results = {};
    for (item in data.faq) {
        if (item.toLowerCase().includes(term.toLowerCase()) || data.faq[item].toLowerCase().includes(term.toLowerCase())) {
            results = {...results,[item]:data.faq[item]};
        }
    }
    return {faq: results};
}

Upvotes: 0

marmeladze
marmeladze

Reputation: 6562

Assume your object is

const that = {
  "faq": {
    "How old is John?": "John is 30",
    "What color are Anna's eyes": "They are Blue",
    "What's the name of Anna and John child?": "His name is jack"
  }
}

You can filter keys containing "John"

Object.keys(that.faq).filter(e => e.indexOf("John") >= 0)

"Anna"

Object.keys(that.faq).filter(e => e.indexOf("Anna") >= 0)

et cetera

Upvotes: 0

Related Questions