简体   繁体   English

将 json 数据转换为 javascript 中特定格式的数组

[英]convert json data to array in javascript for specific format

Im working on leaflet map, I need to place some points in the map.我正在研究 leaflet map,我需要在 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.. 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:只需 map 您的obj结果:

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

Note that your obj is not an object .请注意,您的obj不是object It's an array .这是一个数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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