Currently, I have an array of objects
const bankData = [
{
"bank": "Chase",
"monthEndBalance": "72,175.88",
"bankStatementDate": "2020/10/31"
},
{
"bank": "Chase",
"monthEndBalance": "93,412.79",
"bankStatementDate": "2020/07/31"
},
{
"bank": "Chase",
"monthEndBalance": "88,175.88",
"bankStatementDate": "2020/11/30"
},
{
"bank": "Chase",
"monthEndBalance": "102,200.12",
"bankStatementDate": "2020/12/31"
},
{
"bank": "Goldman Sachs",
"monthEndBalance": "72,276.12",
"bankStatementDate": "2020/11/30"
},
{
"bank": "Goldman Sachs",
"monthEndBalance": "52,276.12",
"bankStatementDate": "2020/10/31"
},
{
"bank": "Goldman Sachs",
"monthEndBalance": "102,276.12",
"bankStatementDate": "2020/12/31"
},
{
"bank": "Wells Fargo",
"monthEndBalance": "33,276.12",
"bankStatementDate": "2020/11/30"
},
{
"bank": "Wells Fargo",
"monthEndBalance": "12,276.12",
"bankStatementDate": "2020/10/31"
},
{
"bank": "Wells Fargo",
"monthEndBalance": "150,276.12",
"bankStatementDate": "2020/12/31"
}
]
I'm looking for the desired output where each bank will be filtered out into it's own array of objects.
E.g
const bankOne = [
{
"bank": "Chase",
"monthEndBalance": "72,175.88",
"bankStatementDate": "2020/10/31"
},
{
"bank": "Chase",
"monthEndBalance": "93,412.79",
"bankStatementDate": "2020/07/31"
},
{
"bank": "Chase",
"monthEndBalance": "88,175.88",
"bankStatementDate": "2020/11/30"
},
{
"bank": "Chase",
"monthEndBalance": "102,200.12",
"bankStatementDate": "2020/12/31"
}
]
I have a rudimentary implementation with Array.filter
that does it but it is not scalable as more banks are added and it would require a code change for each new bank, what would be the ideal method that's able to filter each bank into its own unique array of objects?
const chaseBank = bankData.filter(el => el.bank === "Chase")
const goldManBank = bankData.filter(el => el.bank === "Goldman Sachs")
const wellsFargoBank = bankData.filter(el => el.bank === "Wells Fargo")