how to create React search filter for search multiple object key values
You can keep a value filter
in your component state and use that to see if it is contained as a substring in any of the array element properties.
Example
class App extends React.Component {
state = {
filter: "",
data: [
{
fname: "Jayne",
lname: "Washington",
email: "[email protected]",
gender: "female"
},
{
fname: "Peterson",
lname: "Dalton",
email: "[email protected]",
gender: "male"
},
{
fname: "Velazquez",
lname: "Calderon",
email: "[email protected]",
gender: "male"
},
{
fname: "Norman",
lname: "Reed",
email: "[email protected]",
gender: "male"
}
]
};
handleChange = event => {
this.setState({ filter: event.target.value });
};
render() {
const { filter, data } = this.state;
const lowercasedFilter = filter.toLowerCase();
const filteredData = data.filter(item => {
return Object.keys(item).some(key =>
item[key].toLowerCase().includes(lowercasedFilter)
);
});
return (
<div>
<input value={filter} onChange={this.handleChange} />
{filteredData.map(item => (
<div key={item.email}>
<div>
{item.fname} {item.lname} - {item.gender} - {item.email}
</div>
</div>
))}
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>