简体   繁体   English

将逗号分隔的字符串数组转换为对象数组

[英]Convert array of comma separated strings to array of objects

I want to convert an array to object with key-value pairs. 我想将数组转换为具有键值对的对象。

The array is like this: 数组是这样的:

latLngArray = [ '52.12,-106.65', '53.53,-113.50' ]

I want to convert it to array of objects like this: 我想将其转换为这样的对象数组:

[{lat: 52.12, lng: -106.65}, {lat: 53.53, lng: -113.50}]

I tried to separate each element in the array with split but didn't help. 我试图用split分隔数组中的每个元素,但没有帮助。

How can I convert this array to an object? 如何将该数组转换为对象?

Try this with simple map() 使用简单的map()尝试一下

 latLngArray = ['52.12,-106.65', '53.53,-113.50']; result = latLngArray.map(coords => { const [lat, lng] = coords.split(','); return { lat, lng }; }); console.log(result) 

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

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