I’ve been studying Javascript for about a year and currently learning nodeJS and Express. I’m trying to resolve a test question, which involves converting a text file into key value pairs.
The provided text file looks like this (with 300 lines):
enter image description here
I want a conversion that creates an array or JSON that looks, more or less, like this:
{283: 'land'}
{45: 'sun'}
{149: 'too'}
{258: 'huge'}
{161: 'dont'}`
The first column items become the keys and the second column items become the values.
This result will become a constant where I can call on the number (key) and get the word (value).
I’ve succeeded at converting to JSON with headers at the top of the columns, then removing all of the unwanted info to result in just the number and the word.
I had to edit the text file to make perfectly spaced columns and add the headers:
json_num json_word
283 land
45 sun
149 too
258 huge
161 dont...
Here is the code I’m using:
enter image description here
And the result is roughly this:
1 design
3 all
6 skill
10 whole
15 check
21 deal
28 wish
36 visit...
This solution does not strike me as elegant in any way. I’m particularly focused on a simpler way to convert the key/value pairs from the text file, but of course I will accept any critique on the entire plan of action for the staircase exercise if you have something to share.
I’ve searched every combination of criteria I could think of to find an answer, but the key/value pairs seem to always need headers. I’m probably not using the right terminology.
Please excuse my fledgeling use of terminology and do ask for more info if I left out something critical. Thanks!
Lisa Lung is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.