I have a object that simulate a database relationships with multiple arrays and i want a sale that have a music genre (in this case, the sale with id: 1), but my code is returning all sales. I use the filter and map methods to simulate a select and join from SQL.
const db = {
events: [
{
id: "1",
name: "Rock in Rio 2020",
genre: "Music",
},
{
id: "2",
nome: "World Cup 2022",
genre: "Sport",
},
],
tickets: [
{
id: "1",
eventId: "1",
name: "Day 3",
description: "Music Concert",
},
{
id: "2",
eventId: "1",
name: "France x Brazil",
description: "Football Match",
},
],
sales: [
{
id: "1",
ticketId: "1",
value: "100",
},
{
id: "2",
ticketId: "2",
value: "200",
},
],
};
const musicSales = db.sales.filter((sale) => {
return db.tickets.map((ticket) => {
return db.events.map((event) => {
return (
sale.ticketId === ticket.id &&
event.id === ticket.eventId &&
event.genre === "Music"
);
});
});
});
console.log(musicSales);
Any idea how to fix this?