i'm trying to filter array that the results of value {purchase} id contain in array of {products.transactions.purchase} id. I want to filter by transaction which have same purchase id value.
the data:
var data = [
{
"purchase": "5ace99b014a759325776aabb",
"products":
[{
"transactions":
[{
"purchase": "5ace99b014a759325776aabb",
"price": "25"
},
{
"purchase": "5ace99d714a759325776aac4",
"price": "23"
}],
"_id": "5ace995914a759325776aab0",
"product_name": "Milk",
},
{
"transactions":
[{
"purchase": "5ace99b014a759325776aabb",
"price": "20"
},
{
"purchase": "5ace99d714a759325776aac4",
"price": "15"
}],
"_id": "5ace995c14a759325776aab1",
"product_name": "Ketchup",
}]
},
{
"purchase": "5ace99d714a759325776aac4",
"products":
[{
"transactions":
[{
"purchase": "5ace99b014a759325776aabb",
"price": "22"
},
{
"purchase": "5ace99d714a759325776aac4",
"price": "21"
}],
"_id": "5ace995914a759325776aab0",
"nama_produk": "Milk",
},
{
"transactions":
[{
"purchase": "5ace99b014a759325776aabb",
"price": "14"
},
{
"purchase": "5ace99d714a759325776aac4",
"price": "13"
}],
"_id": "5ace995c14a759325776aab1",
"product_name": "Ketchup",
}]
}]
i have been tried but only show child array
function filter() {
let result = []
let filter = data.filter(a => {
return a.products.filter(b => {
return b.transactions.filter(c => {
if (a.purchase == c.purchase) result.push(c)
})
})
})
return result
}
console.log(filter())
// output
> Array [Object
{ purchase: "5ace99b014a759325776aabb", price: "25" }, Object
{ purchase: "5ace99b014a759325776aabb", price: "20" }, Object
{ purchase: "5ace99d714a759325776aac4", price: "21" }, Object
{ purchase: "5ace99d714a759325776aac4", price: "13" }]
how to the output of filtered array like below output:
[{
"purchase": "5ace99b014a759325776aabb",
"products":
[{
"transactions":
[{
"purchase": "5ace99b014a759325776aabb",
"price": "25"
}],
"_id": "5ace995914a759325776aab0",
"product_name": "Milk",
},
{
"transactions":
[{
"purchase": "5ace99b014a759325776aabb",
"price": "20"
}],
"_id": "5ace995c14a759325776aab1",
"product_name": "Ketchup",
}]
},
{
"purchase": "5ace99d714a759325776aac4",
"products":
[{
"transactions":
[{
"purchase": "5ace99d714a759325776aac4",
"price": "21"
}],
"_id": "5ace995914a759325776aab0",
"nama_produk": "Milk",
},
{
"transactions":
[{
"purchase": "5ace99d714a759325776aac4",
"price": "13"
}],
"_id": "5ace995c14a759325776aab1",
"product_name": "Ketchup",
}]
}]
Thank you.
purchaseor_id?