I have a json like this grouped by date and represents the events of the day with title and abstract:
[
{
"groupKey": "2023-10-12",
"events": [
{
"uid": "4bd2d222",
"title": "LEAVE IT TILL LATER",
"abstract": "One of the most valuable pieces of advice we have picked up during our time in this... the only exception to made up words that we can think of..."
},
{
"uid": "f271d19a",
"title": "DARE TO BE DIFFERENT",
"abstract": "It’s tough to stand out in the events business but if you can come up with a good name, you should use it..."
}
]
},
{
"groupKey": "2023-10-17",
"events": [
{
"uid": "22e9d4c5",
"title": "SHORT AND SWEET",
"abstract": "Short and snappy names work best. Something that is catchy, on point... words are..."
},
{
"uid": "e456d19a",
"title": "INITIALS AND ABBREVIATIONS",
"abstract": "Checking initials and abbreviations is something every event planner..."
}
]
},
{
"groupKey": "2023-10-19",
"events": [
{
"uid": "7bbd8c4f",
"title": "PLAY ON WORDS",
"abstract": "Wordplay is great fun but also incredibly difficult to pull off. Double entendre, double meanings..."
},
{
"uid": "LeIaCRXmr",
"title": "TRY TO AVOID MADE UP WORDS",
"abstract": "If you’re trying to name a business event, try to avoid made up words if possible..."
},
{
"uid": "8611ae0e",
"title": "PORTMANTEAU",
"abstract": "Technically, a portmanteau word is a made up word but it is made up of real words combined in creative ways..."
}
]
}
]
I can search in the ‘title’ or ‘abstract’ fields, the search must return an array with the same structure.
For example, if I have to search for "word" the search function will have to return a json like the following:
[
{
"groupKey": "2023-10-12",
"events": [
{
"uid": "4bd2d222",
"title": "LEAVE IT TILL LATER",
"abstract": "One of the most valuable pieces of advice we have picked up during our time in this... the only exception to made up words that we can think of..."
}
]
},
{
"groupKey": "2023-10-17",
"events": [
{
"uid": "22e9d4c5",
"title": "SHORT AND SWEET",
"abstract": "Short and snappy names work best. Something that is catchy, on point... words are..."
}
]
},
{
"groupKey": "2023-10-19",
"events": [
{
"uid": "7bbd8c4f",
"title": "PLAY ON WORDS",
"abstract": "Wordplay is great fun but also incredibly difficult to pull off. Double entendre, double meanings..."
},
{
"uid": "LeIaCRXmr",
"title": "TRY TO AVOID MADE UP WORDS",
"abstract": "If you’re trying to name a business event, try to avoid made up words if possible..."
},
{
"uid": "8611ae0e",
"title": "PORTMANTEAU",
"abstract": "Technically, a portmanteau word is a made up word but it is made up of real words combined in creative ways..."
}
]
}
]
containing ‘word’ in the ‘title’ and ‘abstract’ fields
I don’t know how to use array.filter in a grouped array, can anyone help me? I searched online but didn’t find anything.
Thank you
3
Answers
Use the map method on the data array. For each group, filter its events based on whether the event’s title or abstract contains the search term.
After filtering events for each group, return the group with its filtered events.
After the map, some groups might have no events left. Use the filter method to remove these empty groups.
Reduce the array with filtered items:
You can use the array.filter for the inner array just like you do with the main array, and filter the outer array based on if there are any matching element in the inner array.