简体   繁体   中英

Replace numbers by JavaScript variable

i have a variable in JavaScript called "something" and it has been assigned with the value of a php variable called "output". When i Console.log (something), i get the right output ie, 116.076580833333, -33.8541619444444 (string format) which is the value of "output".

What i want to achieve:
center: [116.076580833333, -33.8541619444444],

Change to

center: [something],

But if i put "something" in the there, my php file gives no output. from what i can see it can only accepts integers. Any help is appreciated! Thanks!

Javascript

var something=<?php echo json_encode($output); ?>;

console.log(something);  // gets the value of $output inside "something"

var map = new mapboxgl.Map({

  

   center: [116.076580833333, -33.8541619444444]  // value of "something" should fill here
  
      
  
});

You could split by comma and map then cast to number with Number

 const something = '116.076580833333, -33.8541619444444' const obj = { center: something.split(',').map(Number) } console.log(obj)

I finally got the answer

JavaScript

var something=<?php echo json_encode($output); ?>;
//console.log(something);

const coordinates = something;
const [longitude, latitude] = coordinates.split(',');

console.log(longitude);
console.log(latitude);

var map = new mapboxgl.Map({

  

  center: [longitude, latitude]
      
  
});

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