简体   繁体   中英

filter and return multiple values of an array

In a react container I'd like to filter multiple values of an array and return the result if any of the values have the filtered data.

My code is returning results only what is the last in my filter list (in this case 'sanskrit'). If I changed the order of the return to: return (sanskrit, english) - then it filters only the 'english' values.

Can someone please advise how I could filter both the 'sanskrit' and 'english' names in my database? Thank you in advance.

Here's my code snippet:

const filteredAsanas = asanas.filter((asana) => {
    const english = asana.english_name.toLowerCase().includes(searchfield.toLowerCase()),
    sanskrit = asana.sanskrit_name.toLowerCase().includes(searchfield.toLowerCase());
    return (english, sanskrit)
});

Here's an example of my json:

{
   "id":1,
   "sanskrit_name":"Navasana",
   "english_name":"Boat",
   "img_url":"placeholder for the image url"
}

You need to return english || sanskrit english || sanskrit

const filteredAsanas = asanas.filter((asana) => {
    const english = asana.english_name.toLowerCase().includes(searchfield.toLowerCase()),
    sanskrit = asana.sanskrit_name.toLowerCase().includes(searchfield.toLowerCase());
    return english || sanskrit
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM