-1

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.

4
  • 1
    Why is "43.6045" a string and 43.60 a number? Also please share what you have already tried? Commented Sep 9, 2022 at 14:00
  • Can be a string or a number, leaflet accept both Commented Sep 9, 2022 at 14:02
  • yeah but if you gonna make a function to create an array with a specific format, then make it simpler, keep them all as numbers or strings, not like the example you gave Commented Sep 9, 2022 at 14:04
  • That's not my question. The input shows latitude to be a string, but the output has one number and one string. I assume this is a typo in the question? Commented Sep 9, 2022 at 14:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.