To combine two arrays side by side, I am following the below procedure but I get
Cannot set Property "account" of undefined.
Here is my code
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;
}
}
}
If I add like this this.newacc = [{}]; I get same error for 2nd if that is this.newacc[i].accounts
What I did mistake here? Or is there any easiest way to combine two arrays side by side? These two arrays are independent in array length and do not carry any join.