Home > Net >  Unnest Javascript Array into JSON
Unnest Javascript Array into JSON

Time:10-30

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?

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related