2

I have this TXT file:

{ input: [0,0], output: [0], id: 232362 }
{ input:[0,1], output: [1], id: 232363 }

I would like to convert it into array of objects in Typescript this way:

[
    { input: [0, 0], output: [0] },
    { input: [0, 1], output: [1] }
];

Could you help me, please?

I am a beginner, sorry.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Jirka
  • 21
  • 2
  • Where is this going to run? In node? In a browser? The "file" part of this makes it more complex than just parsing a string – jcalz Jan 16 '23 at 21:54
  • 1
    You have to use the `fs` module to read the file and parse it into a string. Once you have a string, you should be able to use the `split()` method to make it into an array. For example, `str.split('\n')` should split the `str` string into an array based on newline characters. Furthermore, you can use `JSON.parse()` to convert the string objects into Javascript objects. – Zo-Bro-23 Jan 17 '23 at 04:07
  • Does this answer your question? [NodeJs read JSON file](https://stackoverflow.com/questions/38325503/nodejs-read-json-file) – Darryl Noakes Jan 17 '23 at 22:43
  • Assuming that each line is a separate object, you would have to split the lines, parse them each separately, and then add them to an array. you could do that with `Array.map` on the array of lines. – Darryl Noakes Jan 17 '23 at 22:46

0 Answers0