I have a list of products as an array of object, something like
[ {
id: 1,
price: 10,
name: 'product',
categories: [
{id: 1, name: 'category 1'} ]
},
{
id: 2,
price: 10,
name: 'product3',
categories: [
{id: 1, name: 'category 1'},
{id: 2, name: 'category 2'},]
},
...
]
And so on.
I'm trying to filter the product based on the category id, which is the best (and most modern) approach?, also since I'm working with Node.js, can it be better to work on a "copy" of the data, so that I always keep the full dataset intact for other uses? (like another filtering, or filter remove)
theArray.filter(product => product.categories.find(category => category.id === desiredId))