Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
get array, separate comma separated values and then recreate array

I have the following array:

[
["Polymeric", "Vehicle Graphics (Basic)"],
["Cast", "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
["Polymeric", "Vehicle Graphics (Part Wrap)"]
]

You'll notice that the 2nd lines of the array's second value has a value separated by a comma. I want to recreate the array like so.

[
["Polymeric", "Vehicle Graphics (Basic)"],
["Cast", "Vehicle Graphics (Part Wrap)"],
["Cast", "Vehicle Graphics (Full Wrap)"],
["Polymeric", "Vehicle Graphics (Part Wrap)"]
]

Here's what I have so far:

const media = [ 
["Polymeric", "Vehicle Graphics (Basic)"],
["Cast", "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
["Polymeric", "Vehicle Graphics (Part Wrap)"]
];

var filteredArray = media.filter(function(r) {return r[1]})

filteredArray.forEach(function(value) {
   var fgh = value.slice(1);
   var cddd = [value.shift()]; // takes first part
   var ttt = fgh.toString()
   var str_array = ttt.split(', ');
      const array3 = cddd.concat(fgh);
     console.log(str_array);
});

I understand I need to use concat, split and then finally push it back but whatever I try I'm not getting the result I need so I need help and now I'm lost?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you can ensure your nested arrays will always have two elements then it's quite simple:

const source = [ 
  ["Polymeric", "Vehicle Graphics (Basic)"],
  ["Cast", "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
  ["Polymeric", "Vehicle Graphics (Part Wrap)"]
];

// Flat map instructs that 
// the nested arrays we're going to return
// should be spread onto the root one
source.flatMap(([key, value]) => { // [key, value] de-structs the array passed as a parameter
  // Here we're getting an array of all possible values; 
  // if there's no ", " then we get a single element, 
  // otherwise we get the whole collection of values
  const values = value.split(', ') 

  // Now, for each of the individual values we create the key-value pairs
  return values.map(singleValue => [key, singleValue])
})

Yields: enter image description here

  • Array destructuring JavaScript

  • Docs for flatMap

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda