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

118
Visualizações
how to compare and filteredout data with next array special characters string reference in javascript

here is my question how to categorize the array data with the help of other array data elements im confusing how to compare the two array data sets with the common value

how can i access the animal# in referenceBasedValues and how to compare with referenceData elements

 const referenceData = ['animal#', 'flower#' , 'Cats#', 'dogs/colorDogs/']

    const referenceBasedValues = [{ref : 'animal#cow', value : 'Cow'},
                  {ref : 'animal#pig', value : 'Pig'},
                  {ref : 'animal#hourse', value : 'Hourse'},
                  {ref : 'flower#rose', value : 'Rose'},
                  {ref : 'flower#leafRose', value : 'Leaf Rose'},
                  {ref : 'Cats#bigcat', value : 'Big cat'},
                  {ref : 'Cats#smallcat', value : 'Small Cat'},
                  {ref : 'dogs/colorDogs/whiteDog', value : 'White dog'},
                  {ref : 'dogs/colorDogs/BlackDog', value : 'Black dog'},]

Im expecting the output like this

  const finalData = [{reference : 'animal#' , values :['Cow' , 'Pig' ,'Hourse']},
                     {reference : 'flower#' , values :['Rose' , 'Leaf Rose' ,'Hourse']},
                     {reference : 'Cats#' , values :['Big cat' , 'Small Cat']},
                     {reference : 'dogs/colorDogs/' , values :['White dog','Black dog']}
                    ]

im able to accesing animal#, flower#, Cats# with substring(0 , referenceData[i].reference.lastIndexOf("#"))

but im not able to accessing dogs/colorDogs/ word in array while filtering

If any one give answer for this one so appreciatable

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

0

// v1

const finalData = referenceData.map(reference => {
  const re = new RegExp('^' + reference);
  const values = referenceBasedValues.filter(base => re.test(base.ref)).map(base => base.value);
  return {
    reference,
    values,
  };
});

// v2

const re = new RegExp(`^(${referenceData.join('|')})`);
const finalData = Object.entries(referenceBasedValues.reduce((obj, { ref, value }) => {
  const match = re.exec(ref);
  if (match) {
    let key = match[0];
    obj[key] ??= [];
    obj[key].push(value);
  }
  return obj;
}, {})).map(([reference, values]) => ({ reference, values }));
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