I have two arrays of objects, one contains Shops with it's id, name etc. and another with Products each product has shop_id property in it and i have a filter function which by passing Shop id returns all Products of that shop. The issue is that there could be Shops without items and i would remove that Shops from the array.
So i was going to use .reduce to do it with a .filter on Products Array but i can't get on how i can return the array with only Shops that has some items
i was trying to do something like this
var shops = [ { id: 123, desc: 'Pippo' }, { id: 124, desc: 'Pluto' }, { id: 125, desc: 'Gianni' } ]
const products = [ { id: 1, desc: 'Car', shop: 123 }, { id: 2, desc: 'Jet', shop: 123 }, { id: 3, desc: 'Pizza', shop: 124 } ]
shops = shops.reduce((a,b) => {
products.filter((item) => item.menu === b.id).length ? b : a
})
console.log(shops) // shouldn't return shop 125 as no items in products for its id.
[<>]and provide a minimal reproducible example.reduceis in error as i can't get how to return the correct value from it