将逗号分隔的数字字符串转换成二维数组。

huangapple go评论70阅读模式
英文:

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 = &quot;547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447&quot;;

const arr = original
  .match(/\d+,\d+/g)
  .map(substr =&gt; substr.split(&#39;,&#39;).map(Number));
console.log(arr);

<!-- end snippet -->

答案2

得分: 3

你可以使用 splitreduce 方法与 % 运算符来创建所需的结果。

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 = &quot;547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447&quot;;
const result = original.split(&#39;,&#39;).reduce((r, e, i) =&gt; {
  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 = &quot;547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447&quot;;

var array = Array.from(original.matchAll(/(\d+),(\d+)/g), 
                       ([, ...m]) =&gt; 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 = &quot;547,449,737,452,767,421,669,367,478,367,440,391,403,392,385,405,375,421,336,447&quot;;

const arrayToMatrix = (array, columns) =&gt; Array(Math.ceil(array.length / columns)).fill(&#39;&#39;).reduce((acc, cur, index) =&gt; {
  return [...acc, [...array].splice(index * columns, columns)]
}, []);

const result = arrayToMatrix(original.split(&#39;,&#39;),2);

console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 17:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定