gpt4 book ai didi

javascript - 将字符串转换为数组数组

转载 作者:行者123 更新时间:2023-11-30 07:53:35 27 4
gpt4 key购买 nike

我有一串数据,看起来像这样

0E-11 GERBER CA 960350E-110.0215.500000000 0E-12 TEHAMA CA 960900E-11214.800000000

我想把这个字符串转换成数组的数组。

请注意,在每 4 个元素之后,这个数组应该被分成新的数组,最终结果应该是这样的:

期望结果:

this.tblData: Array(2)
0: ["0E-11,"GERBER", "CA", "960350E-110.0215.500000000"]
1:["0E-12", "TEHAMA", "CA" ,"960900E-11214.800000000"]

谢谢

最佳答案

您可以使用 remainder运算符和该字符串上的 forEach 循环以构建数组数组,其中每 n 个步骤创建每个嵌套数组:

var result = [];

"0E-11 GERBER CA 960350E-110.0215.500000000 0E-12 TEHAMA CA 960900E-11214.800000000".split(" ").forEach(function(element, index, array) {
if( (index + 1) % 4 === 0) {
result.push([
array[index-3],
array[index-2],
array[index-1],
array[index]
]);
}
});

console.log(result);

关于javascript - 将字符串转换为数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46699152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com