我有这样的文本文件:
'Ontario', 51.567841, -86.902662 'North Dacota',48.112188, -100.152173 'South Dacota',45.741832, -99.251294 'Montana',47.950558, -106.875805
我需要将它以完整的代码存储在JavaScript中的数组中,如下所示:
var locations = [
['Ontario', 51.567841, -86.902662],
['North Dacota',48.112188, -100.152173],
['South Dacota',45.741832, -99.251294],
['Montana',47.950558, -106.875805]
];
答案 0 :(得分:0)
试试这个
var str = "'Ontario', 51.567841, -86.902662 'North Dacota',48.112188, -100.152173 'South Dacota',45.741832, -99.251294 'Montana',47.950558, -106.875805";
var formattedArray = str.replace(/,\s?/g, ',').replace(/\s(')\b|\b(')\s/g, '***').split('***');
var result = formattedArray.map(function (value) {
return value.replace(/'/g, '').split(',');
});
alert(JSON.stringify(result));