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

177
Views
split and flatten array of comma separated strings

What is the best way to convert:

const a = ["jan,feb,may", "apr,may,sept,oct", "nov,jan,mar", "dec", "oct,feb,jan"]

to

const res = ["jan","feb","may","apr","may","sept","oct","nov","jan","mar","dec","oct","feb","jan"]

PS: in javascript, I tried using array.reduce() and split method. but that didn't work. here is what I tried

let flat = arr.reduce((arr, item) => [...arr, ...item.split(',')]);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Using Array#flatMap and String#split:

const a = ["jan,feb,may", "apr,may,sept,oct", "nov,jan,mar", "dec", "oct,feb,jan"];

const res = a.flatMap(e => e.split(','));

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

In this case you could just join and split again:

const a = ["jan,feb,may", "apr,may,sept,oct", "nov,jan,mar", "dec", "oct,feb,jan"];

const res = a.join().split(',');

console.log(res);

Or implicitly joining:

const a = ["jan,feb,may", "apr,may,sept,oct", "nov,jan,mar", "dec", "oct,feb,jan"];

const res = "".split.call(a, ",");

console.log(res);

Or implicitly joining when splitting with a regex method:

const a = ["jan,feb,may", "apr,may,sept,oct", "nov,jan,mar", "dec", "oct,feb,jan"];

const res = /,/[Symbol.split](a);

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!