Justin
Justin

Reputation: 331

Unnest Javascript Array into JSON

I have created a Javascript array that I need to reformat into Json so that I can display it in a table.

I am trying to take the following javascript array:

[
  {
    "name": "ticker",
    "value": "V"
  },
  {
    "name": 5,
    "value": -0.09453565931246788
  },
  {
    "name": 10,
    "value": -0.08185562540645996
  },
  {
    "name": 90,
    "value": -0.1023591207807445
  },
  {
    "name": 120,
    "value": -0.03745328055393815
  },
  {
    "name": 250,
    "value": 0.13101799630844946
  }
]

And transform it into:

const myTableData = [
  {"name": "ticker", "value": V},
  {"name": "5", "value": -0.09453565931246788},
  {"name": "10", "value": -0.08185562540645996},
  {"name": "90", "value": -0.1023591207807445},
  {"name": "120", "value": -0.03745328055393815},
  {"name": "250", "value": 0.13101799630844946}
];

How can I do this?

Upvotes: 0

Views: 45

Answers (1)

TBA
TBA

Reputation: 2027

Just use JSON.stringify

var myArray = [
  {
    "name": "ticker",
    "value": "V"
  },
  {
    "name": 5,
    "value": -0.09453565931246788
  },
  {
    "name": 10,
    "value": -0.08185562540645996
  },
  {
    "name": 90,
    "value": -0.1023591207807445
  },
  {
    "name": 120,
    "value": -0.03745328055393815
  },
  {
    "name": 250,
    "value": 0.13101799630844946
  }
];

var myJsonString = JSON.stringify(myArray);
console.log(myJsonString);

Upvotes: 1

Related Questions