最佳答案
尝试使用 array.prototype.map 来简化您的工作,
var newArray = arr.map(function(str){
return JSON.parse("{" + str.substring(0,str.length - 1).replace(/lat/,'"lat"').replace(/lng/,'"lng"') + "}")
});
此外,如果该数组中的字符串格式正确,则可以删除一些不必要的 .replace()
,从而提高性能。
DEMO
关于javascript - 将字符串数组转换为对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31994643/