简体   繁体   中英

convert json data to array in javascript for specific format

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:

在此处输入图像描述

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:

在此处输入图像描述

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

Thanks you, have a nice day.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM