Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add new column in javascript array

I have an array containing three columns like this:

data.push({
  country: new Date(),
    newSales: Math.random() * 1000,
     expenses: Math.random() * 5000
 });

Now, on button click, I want to add a new column in it. Can anyone let me know how we can do it?

like image 665
Ashish Avatar asked Dec 15 '22 17:12

Ashish


1 Answers

You could iterate though the data array and add key & value to each element.

data[0]["foo"] = bar; // this can be useful if the key is not constant

or

data[0].foo = "bar"
like image 128
Sruti Avatar answered Jan 01 '23 20:01

Sruti