Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
How to convert file contents into strings?

I have a txt file that looks like this:

1||fun||ball
2||job||hammer
3||run||feet

I want to load that into my code and convert it to:

<dt>1</dt><dd>fun <img src="ball.png"></dd>
<dt>2</dt><dd>job <img src="hamm.png"></dd>
<dt>3</dt><dd>run <img src="feet.png"></dd>

I tried like this:

const openrooms = `1||fun||ball
     2||job||hamm
     3||run||feet`;

const roomlist = openrooms.split('\n');

for (var rooms in roomlist) {
  room = roomlist[rooms].split('||');
  for (var {roomid, name, icon} in room) {
    result = `<dt>${room[roomid]}</dt><dd>${room[name]} <img src="${room[icon]}"></dd>`;
  }
}
document.getElementById("destination").innerHTML = result;
<div id="destination"></div>

all I get is undefined. Why is that? Please try the fiddle below:

https://jsfiddle.net/db6hy8a0/

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try:

const openrooms = `1||fun||ball
     2||job||hamm
     3||run||feet`;

const roomlist = openrooms.split('\n');

const result = roomlist.map(e => {
  var split = e.split('||')
  return `<dt>${split[0]}</dt><dd>${split[1]} <img src="${split[2]}.png"></dd>`
}).join('')

document.getElementById("destination").innerHTML = result;
<div id="destination"></div>

about 4 years ago · Juan Pablo Isaza Report

0

The second loop attempts to destructure the strings from the second split.

Try something closer to:

const openRooms = `1||fun||ball
2||job||hammer
3||run||feet`
 
const rooms = openRooms.split('\n')

result = ""
rooms.forEach(room => {
  const [id, name, icon] = room.split('||')
  result += `${id} room: ${name} - ${icon}\n`
})

console.log(result)

(Although why not just store them in an object in the first place?)

about 4 years ago · Juan Pablo Isaza Report

0

const openrooms = `1||fun||ball
 2||job||hamm
 3||run||feet`;


const result = openrooms.split("\n").map((row)=>{
const items = row.split("||");
return `<dt>${items[0]}</dt><dd>${items[1]} <img src="${items[2]}.png"></dd>`})


document.getElementById("destination").innerHTML = result;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!