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

161
Views
How to make array from input

I have array in txt file and read this with

   fs.readFileSync("./input.txt")

When i wrap it in console.log i get(since it is written in the file itself):

    1 2 3 
    100 5000 

I would have the array:

['1','2','3','100','5000']

Placement of the array in the input file should not change. Suggest how to do it, please.

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

0

You can use regex to split words: \w+

let a = `    1 2 3 
    100 5000 `;
    
console.log(a.match(/\w+/g))

To read your file and split it:

fs.readFileSync("./input.txt").match(/\w+/g)
about 4 years ago · Juan Pablo Isaza Report

0

One way, load it then, split by lines, then on each line split by space then flatten it, then filter out empty values.

let str = `1 2 3 
    100 5000 0`; // added 0 to show filter(Boolean) wont remove
    
console.log(str.split('\n').map(v => v.split(' ')).flat().filter(Boolean))

Result:

[
  "1",
  "2",
  "3",
  "100",
  "5000",
  "0"
]
about 4 years ago · Juan Pablo Isaza Report

0

You could split the string by a space, then filter out the falsy items in the resulting array.

const input = `    1 2 3 
    100 5000 `;

let res = input.split(" ").filter(e => e.trim())
console.log(res)

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!