Home > OS >  convert json data to array in javascript for specific format
convert json data to array in javascript for specific format

Time:09-10

Im working on leaflet map, I need to place some points in the map. I made API in PHP that output data i want in json and i get it in javascript, but i need to make good format to work with leaflet..

   async function get_data() {
        let obj;
        const res = await fetch('api.php')
        obj = await res.json();
        console.log(obj)
    }

    get_data();

I get this result in console:

enter image description here

but what I need is

let points = [
    ["43.6045", 1.444, "point 1"],
    [43.60, 1.444, "point 2"],
];

should you be like that in console:

enter image description here

if someone have any idea how to do this it would really help me!

Thanks you, have a nice day.

CodePudding user response:

Just map your obj result:

const points = obj.map(
    ({latitude, longitude, denomination}) => [latitude, longitude, denomination]
);

Note that your obj is not an object. It's an array.

  • Related