When attempting to merge two arrays side by side, I followed the procedure below but encountered the following error:
Cannot set Property "account" of undefined.
This is the code in question:
acs = [
{
"account": "Cash In Hand",
"liabilities": 0,
"assets": 8031597
},
{
"account": "Tax Acs",
"liabilities": 988363.72,
"assets": 0.98
},
{
"account": "Sundry Debtor",
"liabilities": 0,
"assets": 551
},
{
"account": "Sundry Creditor",
"liabilities": 0,
"assets": 0
}
];
acd: any;
acc: any;
newacc: any;
constructor() { }
ngOnInit() {
this.acd = this.acs.filter(f => f.liabilities !== 0);
this.acc = this.acs.filter(f => f.assets !== 0);
const bigger = this.acd.length > this.acc.length ? this.acd.length : this.acc.length;
this.newacc = [];
for (let i = 0; i < bigger; i++) {
if (this.acd.length > i) {
this.newacc[i].account = this.acd[i].account;
this.newacc[i].liabilities = this.acd[i].liabilities;
}
if (this.acc.length > i) {
this.newacc[i].accounts = this.acc[i].account;
this.newacc[i].assets = this.acc[i].assets;
}
}
}
Adding this.newacc = [{}];
results in the same error occurring for the second if
statement as well - specifically at this.newacc[i].accounts
.
What mistake may have been made here? Is there a simpler method to combine these independent arrays side by side, considering their differing lengths and lack of associated data?