Combining two arrays side by side in Angular 7

33
January 11, 2019, at 12:40 PM

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.

Answer 1

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);

Answer 2

Try this way to merge two array into one array.

this.array1.push(...this.array2);
Answer 3

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));
READ ALSO
Mongoose: Remove Document and all references completely

Mongoose: Remove Document and all references completely

I was wonderingI have a lot of coding to do with a function to remove a document from the database

16
hyperledger fabric calling enrollment endpoint failed with error

hyperledger fabric calling enrollment endpoint failed with error

I was trying to deploy hyperledger fabric on multiple hosts using this code and used the balance transfer chaincode using node-SDKWhile I tried to enroll the user, I got the following error:

39
HTML Table to Excel Issue

HTML Table to Excel Issue

I'm using the TableExportjs API and PNP

21
How to create python application like pipenv or django-admin or pytest etc&hellip;?

How to create python application like pipenv or django-admin or pytest etc…?

I am writing a pip module that has to run from command lineI need some steps to create python application

42