我有一个可变字符串mymvariable
其中包含 :
\n\n864827,,,34200,S\xc3\xa8te ,,,445958,,,30220,AIGUES MORTES,,,169807,,,34570,PIGNAN,,,546049,,,13006,MARSEILLE,,\nRun Code Online (Sandbox Code Playgroud)\n\n我想将此字符串转换为如下数组:
\n\n1 864827,,,34200,S\xc3\xa8te ,,, \n2 445958,,,30220,AIGUES MORTES,,, \n3 169807,,,34570,PIGNAN,,, \n4 546049,,,13006,MARSEILLE,,\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试使用:
\n\nvar array = myvariable.split(",");\nRun Code Online (Sandbox Code Playgroud)\n\n但是当我做一个 array[0] 时,它给了我这里的第一个字段:
\n\n\n\n\n864827
\n
当我像这样执行 array[0] 时,我希望有第一行:
\n\n\n\n\n1 864827,,,34200,S\xc3\xa8te ,,,
\n
是否可以像这样格式化字符串?
\n\n感谢您
\n您可以拆分数组并减少数组并为每 7 个项目构建子数组。
\n\nvar string = \'864827,,,34200,S\xc3\xa8te ,,,445958,,,30220,AIGUES MORTES,,,169807,,,34570,PIGNAN,,,546049,,,13006,MARSEILLE,,\',\r\n array = string\r\n .split(\',\')\r\n .reduce((r, s, i) => r.concat([i % 7 ? r.pop().concat(s) : [s]]), []);\r\n\r\nconsole.log(array[0]);\r\nconsole.log(array);Run Code Online (Sandbox Code Playgroud)\r\n.as-console-wrapper { max-height: 100% !important; top: 0; }Run Code Online (Sandbox Code Playgroud)\r\n| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |