0

I am creating searchbar to filter key value data objects. I am getting filter is not a function error while doing search. Is there any other function to filter in key value pair objects ?

Data :-

{
  meta_city : {
    label: "City"
    values: (5) ["DL", "KA", "GJ", "MH", "UP"]
   },
  meta_country : {
    label: "Country"
    values: (5) ["IN", "US", "CA"]
   }
}

Handle search (filterData data is local state) :-

const handleSearchFilter = (event) => {
    const searchWord = event.target.value;
    const newFilter = Object.keys(filterData).map((key) => {
      filterData[key].filter((value) => {
        return value.includes(searchWord);
      });
    });
    setFilterData(newFilter);
  };
<div className="mp-input-field-container">
     <input
       className="mp-input"
       placeholder="Search"
       onChange={handleSearchFilter}                    
     />
</div>
3
  • Are you trying to filter according to the values field of item in the object? Commented Oct 18, 2021 at 13:35
  • @gilamran yes .. Commented Oct 18, 2021 at 13:36
  • What are you expecting the result to look like? Commented Oct 18, 2021 at 13:38

2 Answers 2

1

You should use reduce like this:

const handleSearchFilter = (event) => {
  const searchWord = event.target.value;
  const newFilter = Object.keys(filterData).reduce((result, key) => {
    if (filterData[key].values.includes(searchWord)) {
      result.push(filterData[key]);
    };
    return result;
  }, []);
  setFilterData(newFilter);
};

In this example I'm returning an array result. you can return an object if you want.

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

2 Comments

it is giving this error > TypeError: Cannot read property 'includes' of undefined
I think reduce is overkill in this situation. Add a "?." before "includes" to handle the case there is an entry without the "values" property. see my answer
0

Filter does not exist on a string type. When filterData[key] is called, key has a value of label. filterData["label"] returns a string "City".

try

const searchWord = (word) => Object.values(filterData).filter((data) => data.values?.includes(word));

const handleSearchFilter = (event) => {
  const word = event.target.value;
  const [newFilter] = searchWord(word)
  setFilterData(newFilter);
}

searchWord returns if you search "DL"

[
  {
    label: 'City',
    values: [ 'DL', 'KA', 'GJ', 'MH', 'UP' ]
  }
]

Was that the result you were looking for?

Here is the code snippet to prove the solution works:

var filterData = {
  meta_city : {
    label: "City",
    values: ["DL", "KA", "GJ", "MH", "UP"]
   },
  meta_country : {
    label: "Country",
    values: ["IN", "US", "CA"]
   }
}


const searchWord = (word) => Object.values(filterData).filter((data) => data.values.includes(word));


console.log(searchWord("DL"))

9 Comments

how can i update the state after this ? I mean filterData is an local state
I updated my answer, sorry I thought that was obvious
Your solution returns an array of words, not results...
yes .. it returns nothing ...
@fredericrous TypeError: Cannot convert undefined or null to object
|

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.