Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert json to array

I am trying to convert a json string to an array but i still get json code in return when i parse it.

var slag = [];
                for(var i=0; i < data.length; i++)
                {
                    var string = JSON.stringify(data[i]);
                    var parse = JSON.parse(string)
                    slag.push(parse);
                }
                console.log(slag);

now i still get this output

0: {id: "4", club: "driver", afstand: "230", shot: "straight", uitvoering: "perfect"}
1: {id: "9", club: "ijzer7", afstand: "140", shot: "straight", uitvoering: "perfect"}

but i want something like this in an array

[4, "driver", 240, "straight", "perfect"]
like image 558
Lance Avatar asked Sep 12 '25 19:09

Lance


1 Answers

You need to extract values from the object, use Object.values method after parsing the JSON string.

var parse = Object.values(JSON.parse(string))

FYI : You can do it directly to the object instead of stringifying and parsing(which doesn't makes any sense). So simply iterate and extract values from object using Object.values method.

for(var i=0; i < data.length; i++){
    slag.push(Object.values(data[i]));
}
like image 68
Pranav C Balan Avatar answered Sep 14 '25 10:09

Pranav C Balan