ews2001
ews2001

Reputation: 2187

How can I dynamically add values to my typescript array?

I'm trying to dynamically populate my pieChartColors array, so that in the end my array will look something like this:

public pieChartColors:Array<Color> = [
{
  backgroundColor: '#141C48'
},
{
  backgroundColor: '#FF0000'
},
{
  backgroundColor: '#EFEFEF'
},
...
]

I'm starting with a blank array and have tried a few different ways to get the values added, but none of them are working (my color values are stored without the hash symbol):

public pieChartColors: Array<Color> = [];

public buildPieChart() {
...        
    for (let pie of pieData) {
      // none of these work
      this.pieChartColors.push(backgroundColor: '#'+pie.backgroundColor);
      this.pieChartColors.backgroundColor.push('#'+pie.backgroundColor);
      this.pieChartColors['backgroundColor'].push('#'+pie.backgroundColor);
    }
...
}

BTW, pieData is an object I'm looping through from the database with the backgroundColor values. If I console.log the pie.backgroundColor, the value is available.

Upvotes: 0

Views: 81

Answers (1)

lilezek
lilezek

Reputation: 7344

Just do it like in JavaScript:

this.pieChartColors.push({backgroundColor: '#'+pie.backgroundColor});

Upvotes: 1

Related Questions