1

I have a huge list of data in .txt file. I'm reading it with node.js fs.readFile and when i get the data as data.toString() i get something like this:

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 would need to create a nested arrays splited in five of each row. It should look like this:

[
[[["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"]]]
]

What is the best way of doing this?

Edit:

Current code:

const fs = require('fs');
  fs.readFile(path, async (err, data) => {
    if (err) {
      return console.error(err);
    }
    console.log(data.toString());
  });

File snippet:

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
1
  • 1
    how does the input relate to the output? Commented Jun 29, 2021 at 20:23

3 Answers 3

4

This will give you the expected output of multiple sets of 2 groups of 5 items

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)

Sign up to request clarification or add additional context in comments.

Comments

1

You can do some string manipulation, especially split().

Update

The answer didn't seem to satisfy the OP's question. Here's the actual version:

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);

Old version

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);

4 Comments

Thank you so much!
Did it work? If it did, could you please select it as the answer?
@Kinglish fixed.
@RobinGrundel please see the updated example.
1

split() is what you want to use.

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));

You can even make everything in one line:

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.