如何在js中转换双向声调数组中的字符串

How transform a string in a bidimensonal array in js

提问人:luca_papi 提问时间:10/25/2023 最后编辑:Heretic Monkeyluca_papi 更新时间:10/27/2023 访问量:63

问:

我必须转换/转换“二维数组”中的“值字符串”。

字符串结构

original=LatLng(46.20467, 13.34764),LatLng(46.20467, 13.34767),LatLng(46.20467, 13.34772),LatLng(46.20469, 13.34782),LatLng(46.20471, 13.34786),LatLng(46.20492, 13.34842),LatLng(46.20499, 13.34865),LatLng(46.20502, 13.34888),LatLng(46.20502, 13.34909),LatLng(46.205, 13.34953),LatLng(46.20503, 13.34969),LatLng(46.20516, 13.34992),LatLng(46.20572, 13.35065),LatLng(46.20576, 13.35075),LatLng(46.20576, 13.3508),LatLng(46.20573, 13.35092),LatLng(46.20565, 13.35101)...

数组结构将是

LatLong_completo = [[46.20467, 13.34764],[46.20467, 13.34767],[46.20467, 13.34772],[46.20469, 13.34782],[46.20471, 13.34786],[46.20492, 13.34842],[46.20499, 13.34865],[46.20502, 13.34888],[46.20502, 13.34909],[46.205, 13.34953],[46.20503, 13.34969],[46.20516, 13.34992],[46.20572, 13.35065],[46.20576, 13.35075],[46.20576, 13.3508],[46.20573, 13.35092],[46.20565, 13.35101],[46.20523, 13.35124],[46.20515, 13.35132]...

实际上,我成功地做到了,使用以下代码:

sentences = original.replace(/['LatLng(']/g, ""); // remove LatLng(
sentences = sentences.replace(/[')']/g, "");      // remove )
sentences = sentences.replace(/[' ']/g, "");      // remove (
sentences += ',';                                 // add , at the end

LatLong_base = new Array;
LatLong_completo = new Array;

// loop based on var length
for(i=0;i<sentences.length;i++){

//estrae Latitudine e Longitudine ed elimina i dati appena estratti
position = sentences.search(/,/); // find first comma in string
let Latitudine = sentences.substr(0, position); // using search result extract data
sentences = sentences.slice(position+1);       // using search remove extracted data

position = sentences.search(/,/);
let Longitudine = sentences.substr(0, position);
sentences = sentences.slice(position+1);

// assegna i dati in formato numerico all'Array

 LatLong_base.push(Number(Latitudine)); // 1st data (with number conversion) to array
 LatLong_base.push(Number(Longitudine));// 2nd data (with number conversion) to array

// assegna il nuovo array all'array principale

 LatLong_completo.push( LatLong_base); // dummy array to main array

// reset dummy array
LatLong_base=[]

}

是否存在最佳和快速的解决方案?

JavaScript 字符串 多维数组

评论

0赞 GrafiCode 10/25/2023
那是 chatGPT 代码吗?
0赞 luca_papi 10/26/2023
你为什么认为,代码是使用 chatGPT 生成的?我知道,这不是一个很棒的代码,但这是我自己创建的最好的代码。
0赞 GrafiCode 10/26/2023
因为注释详细级别似乎设置为“exteme”:// remove )

答:

0赞 Lh74vBQrjMJtrYKDUdzK 10/25/2023 #1

这是我将使用的解决方案:

它将字符串拆分为单独的字符串,然后遍历字符串并执行 reg 表达式以匹配括号内的两种模式,例如 ([this],[and this])。

然后,它将这些推送到结果数组中

let original = "LatLng(46.20467, 13.34764),LatLng(46.20467, 13.34767),LatLng(46.20467, 13.34772)";

// Split the string into individual LatLng strings
let latLngStrings = original.split(',');

// Initialize an empty array to store the result
let LatLong_completo = [];

// Iterate through the latLngStrings
latLngStrings.forEach((latLngString) => {
    // Extract the coordinates from the string
    let matches = latLngString.match(/\((.*),\s(.*)\)/);
    if (matches) {
        let lat = parseFloat(matches[1]);
        let lng = parseFloat(matches[2]);
        
        // Create a sub-array and push it to the resultArray
        LatLong_completo.push([lat, lng]);
    }
});

console.log(LatLong_completo);
0赞 Timur 10/25/2023 #2

您可以将 和替换为方括号 (),然后将字符串解析为 JSON。下面是一个示例:LatLng()[ ]

let original = "LatLng(46.20467, 13.34764),LatLng(46.20467, 13.34767),LatLng(46.20467, 13.34772),LatLng(46.20469, 13.34782),LatLng(46.20471, 13.34786),LatLng(46.20492, 13.34842),LatLng(46.20499, 13.34865),LatLng(46.20502, 13.34888),LatLng(46.20502, 13.34909),LatLng(46.205, 13.34953),LatLng(46.20503, 13.34969),LatLng(46.20516, 13.34992),LatLng(46.20572, 13.35065),LatLng(46.20576, 13.35075),LatLng(46.20576, 13.3508),LatLng(46.20573, 13.35092),LatLng(46.20565, 13.35101)"

let LatLong_completo = JSON.parse('[' + original.replaceAll('LatLng(', '[').replaceAll(')', ']') + ']')

console.log(LatLong_completo)

评论

0赞 luca_papi 10/25/2023
这个解决方案正是我需要的,适合我的项目。谢谢
0赞 code_monk 10/25/2023 #3

我就是这样做的

const original = "LatLng(46.20467, 13.34764),LatLng(46.20467, 13.34767),LatLng(46.20467, 13.34772),LatLng(46.20469, 13.34782),LatLng(46.20471, 13.34786),LatLng(46.20492, 13.34842),LatLng(46.20499, 13.34865),LatLng(46.20502, 13.34888),LatLng(46.20502, 13.34909),LatLng(46.205, 13.34953),LatLng(46.20503, 13.34969),LatLng(46.20516, 13.34992),LatLng(46.20572, 13.35065),LatLng(46.20576, 13.35075),LatLng(46.20576, 13.3508),LatLng(46.20573, 13.35092),LatLng(46.20565, 13.35101)";

//  a regex that matches a decimal number
const pattern = /(\d+\.?\d+)/g;

//  split the string on "LatLng"
const completo = original.split("LatLng").map(token => {
  //  capture each decimal string and cast to a number
  return [...token.matchAll(pattern)].map(arr => Number(arr[0]));
}).filter(arr => {
  //  remove invalid regex results
  return arr.length;
})

document.querySelector("code").innerText = JSON.stringify(completo, null, 2);
code {
  width: 450px;
  height: 450px; 
}
<pre><code></code></pre>