We have variable currentCategoryId and array of object
[
{ id: 18, title: "hello world!", categories: [12, 18] },
{ id: 12, title: "hi hi hi", categories: [12] },
{ id: 65, title: "hi there!", categories: [16] },
]
for example const currentCategoryId = 12
and need to return new filter array
[
{ id: 18, title: "hello world!", categories: [12, 18] },
{ id: 12, title: "hi hi hi", categories: [12] },
]
or
const currentCategoryId = 16
return
[
{ id: 65, title: "hi there!", categories: [16] },
]
My code
const postsByCategory = posts.filter(({ categories }) =>
categories.filter(
categoryId => categoryId ===
currentCategoryId
)
)
returned
[
{ id: 18, title: "hello world!", categories: [12, 18] },
{ id: 12, title: "hi hi hi", categories: [12] },
{ id: 65, title: "hi there!", categories: [16] },
]