jquery 如何从一个数组创建另一个数组?

6qftjkof  于 2023-03-22  发布在  jQuery
关注(0)|答案(1)|浏览(137)

我需要创建一个如下所示的数组:

var routes: [{
  locations: [
    [40.749825, -73.090443],
    [42.743244, -71.594375],
    [37.058435, -74.903842]
  ],
  color: 'red',
  opacity: 1
}]

从这里开始:

var locations: [
  {location: "40.749825, -73.090443"},
  {location: "42.743244, -71.594375"},
  {location: "37.058435, -74.903842"}
]

使用javascript/jquery.有人可以帮助我吗?
谢谢

bkhjykvo

bkhjykvo1#

您可以按原样设置coloropacity属性,并在locatoins属性上使用map方法将每个字符串split', '

var locations = [
  {location: "40.749825, -73.090443"},
  {location: "42.743244, -71.594375"},
  {location: "37.058435, -74.903842"}
]

const routes = [{
  color: 'red',
  opacity: 1
}]

routes[0].locations = locations.map(({ location }) => location.split(', '));

console.log(routes)

相关问题