How to convert Array into array objects

88 Views Asked by At

Dears, this is my array:

(3) [{…}, {…}, {…}]
0
: 
{x: 2019, y: '0.50', label: 2019}
1
: 
{x: 2020, y: '0.00', label: 2020}
2
: 
{x: 2021, y: '3.80', label: 2021}
x
: 
0
length
: 
3
[[Prototype]]
: 
Array(0)

I need it in [{ x: 2019, y: 0.50, label: "2019" }, { x: 2020, y: 0.00, label: "2020" }, { x: 2021, y: 3.80, label: "2021" },]; format.

Please help me to do this.

1

There are 1 best solutions below

3
Mishika Dhar On

You can use the JSON.stringify(arrayName) method.

Example:

(3) [{…}, {…}, {…}]
0: {
  x: 2019,
  y: '0.50',
  label: 2019
}
1: {
  x: 2020,
  y: '0.00',
  label: 2020
}
2: {
  x: 2021,
  y: '3.80',
  label: 2021
}
length: 3
[[Prototype]]: Array(0)

After using JSON.stringify(a)output is:

'[{"x":2019,"y":"0.50","label":2019},{"x":2020,"y":"0.00","label":2020},{"x":2021,"y":"3.80","label":2021}]'