英文:
Convert a string of comma separated numbers into a 2D array
问题
I have a string of numbers like so:
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
And I wish to convert this string into a 2D array like so:
[[547, 449], [737, 452], [767, 421], [669, 367], [478, 367], [440, 391], [403, 392], [385, 405], [375, 421], [336, 447]]
But I'm having trouble doing it. I tried using regex:
var result = original.replace(/([-\d.]+),([-\d.]+),?/g, '[$1, $2] ').trim();
But the result was a string of the following and not an array:
[547, 449] [737, 452] [767, 421] [669, 367] [478, 367] [440, 391] [403, 392] [385, 405] [375, 421] [336, 447]
英文:
I have a string of numbers like so:
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
And I wish to convert this string into a 2D array like so:
[[547, 449] [737, 452] [767, 421] [669, 367] [478, 367] [440, 391] [403, 392] [385, 405] [375, 421] [336, 447]]
But I'm having trouble doing it. I tried using regex:
var result = original.replace(/([-\d.]+),([-\d.]+),?/g, '[$1, $2] ').trim();
But the result was a string of the following and not an array:
[547, 449] [737, 452] [767, 421] [669, 367] [478, 367] [440, 391] [403, 392] [385, 405] [375, 421] [336, 447]
答案1
得分: 3
以下是翻译好的部分:
"可能更容易使用全局正则表达式来匹配两个数字段,然后将每个匹配项按逗号分割并转换为数字:"
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
const arr = original
.match(/\d+,\d+/g)
.map(substr => substr.split(',').map(Number));
console.log(arr);
这是代码部分,已经翻译好了。
英文:
Might be easier to use a global regular expression to match two segments of digits, then split each match by comma and cast to number:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
const arr = original
.match(/\d+,\d+/g)
.map(substr => substr.split(',').map(Number));
console.log(arr);
<!-- end snippet -->
答案2
得分: 3
你可以使用 split
和 reduce
方法与 %
运算符来创建所需的结果。
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
const result = original.split(',').reduce((r, e, i) => {
if (i % 2 == 0) r.push([]);
r[r.length - 1].push(e);
return r;
}, [])
console.log(result)
希望这有帮助!
英文:
You could use split
and reduce
methods with %
operator to create the desired result.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
const result = original.split(',').reduce((r, e, i) => {
if (i % 2 == 0) r.push([]);
r[r.length - 1].push(e);
return r;
}, [])
console.log(result)
<!-- end snippet -->
答案3
得分: 2
你可以查找带有逗号分隔的数字,替换它们,添加括号并解析为JSON。
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447",
array = JSON.parse('[' + original.replace(/\d+,\d+/g, '[$&]') + ']');
console.log(array);
英文:
You could look for digits with a comma in between, replace, add brakets and parse as JSON.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447",
array = JSON.parse('[' + original.replace(/\d+,\d+/g, '[$&]') + ']');
console.log(array);
<!-- end snippet -->
答案4
得分: 0
这可能是使用.matchAll()
的一个不错的用例:
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
var array = Array.from(original.matchAll(/(\d+),(\d+)/g),
([, ...m]) => m.map(Number));
console.log(array);
英文:
This could be a nice use case for using .matchAll()
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
var array = Array.from(original.matchAll(/(\d+),(\d+)/g),
([, ...m]) => m.map(Number));
console.log(array);
<!-- end snippet -->
答案5
得分: 0
Using Regex 和 JSON.parse 是比较昂贵的。请使用以下方式将数组转换为矩阵:
const original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
const arrayToMatrix = (array, columns) => Array(Math.ceil(array.length / columns)).fill('').reduce((acc, cur, index) => {
return [...acc, [...array].splice(index * columns, columns)]
}, []);
const result = arrayToMatrix(original.split(','), 2);
console.log(result);
英文:
Using Regex and JSON.parse are costlier. Do it using array to matrix as below
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const original = "547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447";
const arrayToMatrix = (array, columns) => Array(Math.ceil(array.length / columns)).fill('').reduce((acc, cur, index) => {
return [...acc, [...array].splice(index * columns, columns)]
}, []);
const result = arrayToMatrix(original.split(','),2);
console.log(result);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论