从单个数组创建嵌套数组的最快方法?
Fastest way for creating nested arrays from single array?
我在 .txt 文件中有大量数据。我正在用 node.js fs.readFile 阅读它,当我将数据作为 data.toString() 时,我得到这样的东西:
II IY 9C JD 2D QC QY 6Y 3C ID
KI JI 2Y 6Y 5Y QY QI 3Y 7C 6D
TC 3Y 4I 7Y QC 2Y 3I 8C JI KH
AH 8H 5I 4C 9H JD 3H 7I JC AC
我需要创建一个嵌套数组,每行分成五个。它应该是这样的:
[
[[["I","I"] ["I","Y"] ["9","C"] ["J","D"] ["2","D"]], [["Q","C"] ["Q","Y"] ["6","Y"] ["3","C"] ["I","D"]]]
[[["K","I"] ["J","I"] ["2","Y"] ["6","Y"] ["5","Y"]], [["Q","Y"] ["Q","I"] ["3","Y"] ["7","C"] ["6","D"]]]
[[["T","C"] ["3","Y"] ["4","I"] ["7","Y"] ["Q","C"]], [["2","Y"] ["3","I"] ["8","C"] ["J","I"] ["K","H"]]]
[[["A","H"] ["8","H"] ["5","I"] ["4","C"] ["9","H"]], [["J","D"] ["3","H"] ["7","I"] ["J","C"] ["A","C"]]]
]
最好的方法是什么?
编辑:
当前代码:
const fs = require('fs');
fs.readFile(path, async (err, data) => {
if (err) {
return console.error(err);
}
console.log(data.toString());
});
文件片段:
8C TS KC 9H 4S 7D 2S 5D 3S AC
5C AD 5D AC 9C 7C 5H 8D TD KS
3H 7H 6S KC JS QH TD JC 2D 8S
TH 8H 5C QS TC 9H 4D JC KS JS
7C 5H KC QH JD AS KH 4C AD 4S
5H KS 9C 7D 9H 8D 3S 5D 5C AH
6H 4H 5C 3H 2H 3S QH 5S 6S AS
TD 8C 4H 7C TC KC 4C 3H 7S KS
7C 9C 6D KD 3H 4C QS QC AC KH
JC 6S 5H 2H 2D KD 9D 7C AS JS
AD QH TH 9D 8H TS 6D 3S AS AC
2H 4S 5C 5S TC KC JD 6C TS 3C
QD AS 6H JS 2C 3D 9H KC 4H 8S
KD 8S 9S 7C 2S 3S 6D 6S 4H KC
3C 8C 2D 7D 4D 9S 4S QH 4H JD
8C KC 7S TC 2D TS 8H QD AC 5C
3D KH QD 6C 6S AD AS 8H 2H QS
6S 8D 4C 8S 6C QH TC 6D 7D 9D
您可以进行一些字符串操作,尤其是 split()
。
更新
答案似乎没有满足OP的问题。这是实际版本:
const data =
`8C TS KC 9H 4S 7D 2S 5D 3S AC
5C AD 5D AC 9C 7C 5H 8D TD KS
3H 7H 6S KC JS QH TD JC 2D 8S
TH 8H 5C QS TC 9H 4D JC KS JS
7C 5H KC QH JD AS KH 4C AD 4S
5H KS 9C 7D 9H 8D 3S 5D 5C AH
6H 4H 5C 3H 2H 3S QH 5S 6S AS
TD 8C 4H 7C TC KC 4C 3H 7S KS
7C 9C 6D KD 3H 4C QS QC AC KH
JC 6S 5H 2H 2D KD 9D 7C AS JS
AD QH TH 9D 8H TS 6D 3S AS AC
2H 4S 5C 5S TC KC JD 6C TS 3C
QD AS 6H JS 2C 3D 9H KC 4H 8S
KD 8S 9S 7C 2S 3S 6D 6S 4H KC
3C 8C 2D 7D 4D 9S 4S QH 4H JD
8C KC 7S TC 2D TS 8H QD AC 5C
3D KH QD 6C 6S AD AS 8H 2H QS
6S 8D 4C 8S 6C QH TC 6D 7D 9D`;
const lines = data.split("\n"); // Each line will be an array value
let result = []; // Define our result
lines.forEach(item => {
let current = [];
let now = item.split(" ");
let fir = [];
let sec = [];
for(let i = 0; i < now.length; i++) {
if(i < 5) fir.push(now[i].split(""));
else sec.push(now[i].split(""));
}
current.push(fir);
current.push(sec);
result.push(current);
});
console.log(result);
旧版本
const data =
`8C TS KC 9H 4S 7D 2S 5D 3S AC
5C AD 5D AC 9C 7C 5H 8D TD KS
3H 7H 6S KC JS QH TD JC 2D 8S
TH 8H 5C QS TC 9H 4D JC KS JS
7C 5H KC QH JD AS KH 4C AD 4S
5H KS 9C 7D 9H 8D 3S 5D 5C AH
6H 4H 5C 3H 2H 3S QH 5S 6S AS
TD 8C 4H 7C TC KC 4C 3H 7S KS
7C 9C 6D KD 3H 4C QS QC AC KH
JC 6S 5H 2H 2D KD 9D 7C AS JS
AD QH TH 9D 8H TS 6D 3S AS AC
2H 4S 5C 5S TC KC JD 6C TS 3C
QD AS 6H JS 2C 3D 9H KC 4H 8S
KD 8S 9S 7C 2S 3S 6D 6S 4H KC
3C 8C 2D 7D 4D 9S 4S QH 4H JD
8C KC 7S TC 2D TS 8H QD AC 5C
3D KH QD 6C 6S AD AS 8H 2H QS
6S 8D 4C 8S 6C QH TC 6D 7D 9D`;
const lines = data.split("\n"); // Each line will be an array value
let result = []; // Define our result
lines.forEach(item => {
let current = [];
let now = item.split(" ");
let fir = [];
let sec = [];
for(let i = 0; i < now.length; i++) {
if(i < 5) fir.push(now[i].split(""));
else sec.push(now[i].split(""));
}
current.push(fir);
current.push(sec);
result.push(current);
});
console.log(result);
这将为您提供多组 2 组 5 项的预期输出
let data = `II IY 9C JD 2D QC QY 6Y 3C ID
KI JI 2Y 6Y 5Y QY QI 3Y 7C 6D
TC 3Y 4I 7Y QC 2Y 3I 8C JI KH
AH 8H 5I 4C 9H JD 3H 7I JC AC`;
let nested = data.split("\n").map(l => ([
[...l.split(" ").filter((n, i) => i < 5).map(n => n.split(''))],
[...l.split(" ").filter((n, i) => i >= 5).map(n => n.split(''))]
]))
console.log(nested)
// or even more concise
const gset = (grp,setn) => [...grp.split(" ").filter((n, i) => setn == 0 ? (i < 5) : (i>=5)).map(n => n.split(''))]
let nested2 = data.split("\n").map(g => ([gset(g,0), gset(g,1)]))
console.log('More concise version:', nested2)
split()
就是你要用的。
let data = `II IY 9C JD 2D QC QY 6Y 3C ID
KI JI 2Y 6Y 5Y QY QI 3Y 7C 6D
TC 3Y 4I 7Y QC 2Y 3I 8C JI KH
AH 8H 5I 4C 9H JD 3H 7I JC AC`;
const createArray = (str) => {
const splitedByLines = str.split('\n');
const splitedByBlank = splitedByLines.map(line => line.split(' '));
const result = splitedByBlank.map(line => line.map(e => e.split('')));
return result;
}
console.log(createArray(data));
您甚至可以在一行中完成所有内容:
let data = `II IY 9C JD 2D QC QY 6Y 3C ID
KI JI 2Y 6Y 5Y QY QI 3Y 7C 6D
TC 3Y 4I 7Y QC 2Y 3I 8C JI KH
AH 8H 5I 4C 9H JD 3H 7I JC AC`;
const createArray2 = str => str.split('\n').map(line => line.split(' ')).map(line => line.map(e => e.split('')));
console.log(createArray2(data));
我在 .txt 文件中有大量数据。我正在用 node.js fs.readFile 阅读它,当我将数据作为 data.toString() 时,我得到这样的东西:
II IY 9C JD 2D QC QY 6Y 3C ID
KI JI 2Y 6Y 5Y QY QI 3Y 7C 6D
TC 3Y 4I 7Y QC 2Y 3I 8C JI KH
AH 8H 5I 4C 9H JD 3H 7I JC AC
我需要创建一个嵌套数组,每行分成五个。它应该是这样的:
[
[[["I","I"] ["I","Y"] ["9","C"] ["J","D"] ["2","D"]], [["Q","C"] ["Q","Y"] ["6","Y"] ["3","C"] ["I","D"]]]
[[["K","I"] ["J","I"] ["2","Y"] ["6","Y"] ["5","Y"]], [["Q","Y"] ["Q","I"] ["3","Y"] ["7","C"] ["6","D"]]]
[[["T","C"] ["3","Y"] ["4","I"] ["7","Y"] ["Q","C"]], [["2","Y"] ["3","I"] ["8","C"] ["J","I"] ["K","H"]]]
[[["A","H"] ["8","H"] ["5","I"] ["4","C"] ["9","H"]], [["J","D"] ["3","H"] ["7","I"] ["J","C"] ["A","C"]]]
]
最好的方法是什么?
编辑:
当前代码:
const fs = require('fs');
fs.readFile(path, async (err, data) => {
if (err) {
return console.error(err);
}
console.log(data.toString());
});
文件片段:
8C TS KC 9H 4S 7D 2S 5D 3S AC
5C AD 5D AC 9C 7C 5H 8D TD KS
3H 7H 6S KC JS QH TD JC 2D 8S
TH 8H 5C QS TC 9H 4D JC KS JS
7C 5H KC QH JD AS KH 4C AD 4S
5H KS 9C 7D 9H 8D 3S 5D 5C AH
6H 4H 5C 3H 2H 3S QH 5S 6S AS
TD 8C 4H 7C TC KC 4C 3H 7S KS
7C 9C 6D KD 3H 4C QS QC AC KH
JC 6S 5H 2H 2D KD 9D 7C AS JS
AD QH TH 9D 8H TS 6D 3S AS AC
2H 4S 5C 5S TC KC JD 6C TS 3C
QD AS 6H JS 2C 3D 9H KC 4H 8S
KD 8S 9S 7C 2S 3S 6D 6S 4H KC
3C 8C 2D 7D 4D 9S 4S QH 4H JD
8C KC 7S TC 2D TS 8H QD AC 5C
3D KH QD 6C 6S AD AS 8H 2H QS
6S 8D 4C 8S 6C QH TC 6D 7D 9D
您可以进行一些字符串操作,尤其是 split()
。
更新
答案似乎没有满足OP的问题。这是实际版本:
const data =
`8C TS KC 9H 4S 7D 2S 5D 3S AC
5C AD 5D AC 9C 7C 5H 8D TD KS
3H 7H 6S KC JS QH TD JC 2D 8S
TH 8H 5C QS TC 9H 4D JC KS JS
7C 5H KC QH JD AS KH 4C AD 4S
5H KS 9C 7D 9H 8D 3S 5D 5C AH
6H 4H 5C 3H 2H 3S QH 5S 6S AS
TD 8C 4H 7C TC KC 4C 3H 7S KS
7C 9C 6D KD 3H 4C QS QC AC KH
JC 6S 5H 2H 2D KD 9D 7C AS JS
AD QH TH 9D 8H TS 6D 3S AS AC
2H 4S 5C 5S TC KC JD 6C TS 3C
QD AS 6H JS 2C 3D 9H KC 4H 8S
KD 8S 9S 7C 2S 3S 6D 6S 4H KC
3C 8C 2D 7D 4D 9S 4S QH 4H JD
8C KC 7S TC 2D TS 8H QD AC 5C
3D KH QD 6C 6S AD AS 8H 2H QS
6S 8D 4C 8S 6C QH TC 6D 7D 9D`;
const lines = data.split("\n"); // Each line will be an array value
let result = []; // Define our result
lines.forEach(item => {
let current = [];
let now = item.split(" ");
let fir = [];
let sec = [];
for(let i = 0; i < now.length; i++) {
if(i < 5) fir.push(now[i].split(""));
else sec.push(now[i].split(""));
}
current.push(fir);
current.push(sec);
result.push(current);
});
console.log(result);
旧版本
const data =
`8C TS KC 9H 4S 7D 2S 5D 3S AC
5C AD 5D AC 9C 7C 5H 8D TD KS
3H 7H 6S KC JS QH TD JC 2D 8S
TH 8H 5C QS TC 9H 4D JC KS JS
7C 5H KC QH JD AS KH 4C AD 4S
5H KS 9C 7D 9H 8D 3S 5D 5C AH
6H 4H 5C 3H 2H 3S QH 5S 6S AS
TD 8C 4H 7C TC KC 4C 3H 7S KS
7C 9C 6D KD 3H 4C QS QC AC KH
JC 6S 5H 2H 2D KD 9D 7C AS JS
AD QH TH 9D 8H TS 6D 3S AS AC
2H 4S 5C 5S TC KC JD 6C TS 3C
QD AS 6H JS 2C 3D 9H KC 4H 8S
KD 8S 9S 7C 2S 3S 6D 6S 4H KC
3C 8C 2D 7D 4D 9S 4S QH 4H JD
8C KC 7S TC 2D TS 8H QD AC 5C
3D KH QD 6C 6S AD AS 8H 2H QS
6S 8D 4C 8S 6C QH TC 6D 7D 9D`;
const lines = data.split("\n"); // Each line will be an array value
let result = []; // Define our result
lines.forEach(item => {
let current = [];
let now = item.split(" ");
let fir = [];
let sec = [];
for(let i = 0; i < now.length; i++) {
if(i < 5) fir.push(now[i].split(""));
else sec.push(now[i].split(""));
}
current.push(fir);
current.push(sec);
result.push(current);
});
console.log(result);
这将为您提供多组 2 组 5 项的预期输出
let data = `II IY 9C JD 2D QC QY 6Y 3C ID
KI JI 2Y 6Y 5Y QY QI 3Y 7C 6D
TC 3Y 4I 7Y QC 2Y 3I 8C JI KH
AH 8H 5I 4C 9H JD 3H 7I JC AC`;
let nested = data.split("\n").map(l => ([
[...l.split(" ").filter((n, i) => i < 5).map(n => n.split(''))],
[...l.split(" ").filter((n, i) => i >= 5).map(n => n.split(''))]
]))
console.log(nested)
// or even more concise
const gset = (grp,setn) => [...grp.split(" ").filter((n, i) => setn == 0 ? (i < 5) : (i>=5)).map(n => n.split(''))]
let nested2 = data.split("\n").map(g => ([gset(g,0), gset(g,1)]))
console.log('More concise version:', nested2)
split()
就是你要用的。
let data = `II IY 9C JD 2D QC QY 6Y 3C ID
KI JI 2Y 6Y 5Y QY QI 3Y 7C 6D
TC 3Y 4I 7Y QC 2Y 3I 8C JI KH
AH 8H 5I 4C 9H JD 3H 7I JC AC`;
const createArray = (str) => {
const splitedByLines = str.split('\n');
const splitedByBlank = splitedByLines.map(line => line.split(' '));
const result = splitedByBlank.map(line => line.map(e => e.split('')));
return result;
}
console.log(createArray(data));
您甚至可以在一行中完成所有内容:
let data = `II IY 9C JD 2D QC QY 6Y 3C ID
KI JI 2Y 6Y 5Y QY QI 3Y 7C 6D
TC 3Y 4I 7Y QC 2Y 3I 8C JI KH
AH 8H 5I 4C 9H JD 3H 7I JC AC`;
const createArray2 = str => str.split('\n').map(line => line.split(' ')).map(line => line.map(e => e.split('')));
console.log(createArray2(data));