Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda