-2

I need to convert JSON object to javascript array. This my object

var object = 

    [
        {
            "Key": "2019",
            "value": 3
        },
    {
            "Key": "2020",
            "value": 3
        },
    {
            "Key": "2021",
            "value": 3
        }
    ]

i need to weel be my array like this ['2019','2020','2021']

i try to to this

 var data_t = [];
                for (var i in object ) {
                    data_t.push(object );
                }
                console.log(data_t[0]);

But not working !!

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Open
  • 1
  • 3

2 Answers2

0

You can use .map() for this:

var object = [
    {
      "Key": "2019",
      "value": 3
    },
    {
      "Key": "2020",
      "value": 3
    },
    {
      "Key": "2021",
      "value": 3
    }
  ];

console.log(object.map(a => a.Key));
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
-1
var data_t = [];
for (var i in object ) {
    data_t.push(i.Key);
}
            
Fritz
  • 343
  • 1
  • 10