Let's say I have a variable named data
const data = [
{loanname: "Mr X", acnumber: "020000000000001", outstanding: "54000"},
{loanname: "Mrs Y", acnumber: "020000000000087", outstanding: "87000"},
{loanname: "Mr Z", acnumber: "020000000000103", outstanding: "15000"},
{totalaccount: "3", outstanding: "156000"},
{loanname: "David", acnumber: "020000000000091", outstanding: "11000"},
{loanname: "James", acnumber: "020000000001291", outstanding: "4000"},
{totalaccount: "2", outstanding: "15000"},
]
It's an array. I would like to extract data items depending on how many times there are totalaccount. For example, before the first totalaccount, there are three accounts. I would like to extract them.
Also, before the last totalaccount, there are two accounts if we can skip the first totalaccount. I would like to extract information for those two accounts.
To be more precise, how can I convert the array into the following?:
let firstOne = {loanname: "Mr X", acnumber: "020000000000001", outstanding: "54000"},
{loanname: "Mrs Y", acnumber: "020000000000087", outstanding: "87000"},
{loanname: "Mr Z", acnumber: "020000000000103", outstanding: "15000"},
let secondOne ={loanname: "David", acnumber: "020000000000091", outstanding: "11000"},
{loanname: "James", acnumber: "020000000001291", outstanding: "4000"},