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

209
Views
Make an array A with the result of each value of array B splitted by pipe

I have an array of strings. Some of the strings within this array have a pipe character. I would like to split the strings by "|" and store all the unique values into a new array.

What would be an efficient way to get a temporary array with all the splited values in it, without using poor performance loops?

Once I have the temporary array with all the splited values in it, I plan de remove all duplicates like this : var result = [...new Set(result)]

var arr = ["A|B|C","B|A","E|A|D","F"]

// result does not have to be sorted
var expectedResult = ["A","B","C","D","E","F"]

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

0

Use flatMap() and split() to get a single array, and use a Set to retain unique elements:

const array = ["A|B|C","B|A","E|A|D","F"];
const result = [...new Set(array.flatMap(v => v.split('|')))];

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

.join('|') array as a string with pipes between all letters, then .split('|') by the pipe and then remove dupes with Set()

let data = ["A|B|C", "B|A", "E|A|D", "F"];

console.log([...new Set(data.join('|').split('|'))]);

about 4 years ago · Juan Pablo Isaza Report

0

I would go with

const result = arr.map(item => item.split("|")).flat();
const deduped = [...new Set(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!