Combining two arrays side by side in Angular 7

Viewed 5064

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.

2 Answers

You should use push method. The issue is that you're adding elements using C++ syntax like.

 this.newacc[i].account = this.acd[i].account;

As I aforementioned, you could use push method by passing the desired object as parameter.

newacc.push({account:acd[i].account, liabilities : acd[i].liabilities });

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 = acs.filter(f => f.liabilities !== 0);
acc = acs.filter(f => f.assets !== 0);

const bigger = acd.length > acc.length ? acd.length : acc.length, newacc = [];
for (let i = 0; i < bigger; i++) {
  if (acd.length > i)
    newacc.push({account:acd[i].account, liabilities : acd[i].liabilities });
  if (acc.length > i)
    newacc.push({account:acc[i].account, assets : acc[i].assets });
}
console.log(newacc);

Your problem is purely a Typescript issue and nothing angular-ness in it.

Here is a fix to part of your code, the rest I think you can figure out:

interface accnt {
  account: string;
  liabilities: number;
  assets: number;
}

let 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
    }
];

let acd: accnt[] = new Array(4);
let acc: accnt[] = new Array(4);
let newacc: accnt[] = new Array(4);

this.acd = this.acs.filter(f => f.liabilities !== 0);
this.acc = this.acs.filter(f => f.assets !== 0);
alert(JSON.stringify(this.acd));
alert(JSON.stringify(this.acc));
alert(JSON.stringify(this.newacc));

const bigger = this.acd.length > this.acc.length ? this.acd.length : this.acc.length;

for (let i = 0; i < bigger; i++) {
  if (this.acd.length > i) {
    this.newacc[i] =
      {
        account: this.acd[i].account,
        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;
  }*/
}

alert(JSON.stringify(this.newacc));
Related