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

93
Views
Split array by unique items with some items comma separated using javascript

I have two dependent dropdown lists the first list getting the services from array1 and the second filtering a list from array2 based on the results of list 1.

This works as it should in this fiddle: jsfiddle

The issue is the 2nd value has values that are separated with a comma. I need to separate those then check against all the other values in value 2 and only display one instance.

i.e.

"Banner (Premium)", "Banner (Premium), Banner (440gsm)", "Banner (550gsm)"

Would be

"Banner (Premium)", "Banner (440gsm)", "Banner (550gsm)"

Here's my code:

function menu2() {
var serviceValue = document.getElementById("service-type").value;
 var el = document.getElementById("media-type");
 var newArray = media.filter(function(r) {return r[1].includes(serviceValue)})
 var currentlyAdded = [];
 el.innerHTML = "";
 newArray.forEach(function(r) {
 if(currentlyAdded.indexOf(r[1]) === -1) {
 var option = document.createElement('option');
  option.text = r[1];
  el.appendChild(option);
  currentlyAdded.push(r[1]);
  }
 })

}

I believe I need to use .split and .concat but I can't work out how to add it to my script.

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

0

You can use a Set to create an array with unique values.

const str = `Banner (Premium), Banner (Premium), Banner (440gsm)`
const splitted = [...new Set(str.split(`, `))];
console.log(splitted);

about 4 years ago · Juan Pablo Isaza Report

0

A solution with split

const str = '"Banner (Premium)", "Banner (Premium), Banner (440gsm)", "Banner (550gsm)"'


// desired "Banner (Premium)", "Banner (440gsm)", "Banner (550gsm)"

const desired = str.split('"').filter(x => /[A-Z][a-z]/.test(x)).map( x => {
  const splitted = x.split(',');
  return (splitted.length < 2)?splitted[0]:splitted[1];
});



console.log(desired)

I believe that looking for csv you would find pure regex solutions

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!