-2

Hi i would like help in converting a json array into a normal array so i can use to make a chart in chartJs.

This is the data that i need to convert

[
  {
    car: "6.8889",
    eng: "4.6111",
    expec: "2.8889",
    expli: "4.3333",
    gsw: "7.0000",
    imp: "4.0000",
    iss: "9.6667",
    know: "9.3333",
    own: "11.8333",
    ques: "9.3333",
    too: "7.2222",
    wwod: "4.4444",
   __proto__: {},
  }
]

and i would like to convert it into:

[Log] ["4.0000", "9.6667", "4.6111", "9.3333", "11.8333", "9.3333", "2.8889", "4.4444", "4.3333", "7.2222", "7.0000", "6.8889"] (app.min.js, line 4)

i can do this manually like so:

for (_j = 0, _len1 = json3.length; _j < _len1; _j++) {
    l = json3[_j];
    chartData2.push(l.imp, l.iss, l.eng, l.ques, l.own, l.know, l.expec, l.wwod, l.expli, l.too,l.gsw, l.car);
 }

however i have data which can have over 60 different values? so i would like to just do automatically.

I've tried the following examples:

Need to convert json key-value pairs to standard array Converting JSON Object into Javascript array

Any he anyone can give would be awesome

thanks in advance

Community
  • 1
  • 1

1 Answers1

0

You can easily do this, using for each loop of object. Try this:-

var arr = [{
  car: "6.8889",
  eng: "4.6111",
  expec: "2.8889",
  expli: "4.3333",
  gsw: "7.0000",
  imp: "4.0000",
  iss: "9.6667",
  know: "9.3333",
  own: "11.8333",
  ques: "9.3333",
  too: "7.2222",
  wwod: "4.4444",
  __proto__: {}
 }
],
newArr = [];
for (var i = 0; i < arr.length; i++) {
  for (var key in arr[i]) {
    newArr.push(arr[i][key]);
   }
}
console.log(newArr)
Indranil Mondal
  • 2,799
  • 3
  • 25
  • 40