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

316
Visualizações
How to remove underscore from loop items?

I am trying to remove all the _er and _bx from the array, how can I do it? The way I tried doesn't seem to work. I'd like to see a solution where it removes all after _, and aswell only the letter that I put in for e.g remove all _ with er after.

const nullValue = {
  collection: [{
      name: "test_er"
    },
    {
      name: "test_bx"
    },
    {
      name: "fred"
    },
    {
      name: "test_er"
    }
  ]
};

const newArr = []
for (let [key, item] of nullValue.collection.entries()) {
  item.name.replace(/_er/g, '')
  newArr.push(item)
}

console.log(newArr)

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

0

Is this what you're looking for?

const nullValue = {
  collection: [
    {
      name: 'test_er',
    },
    {
      name: 'test_bx',
    },
    {
      name: 'fred',
    },
    {
      name: 'test_er',
    },
  ],
};

nullValue.collection = [
  ...nullValue.collection.map(item => ({
    name: item.name.replace(/_.*$/, ''),
  })),
];

console.log(nullValue);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can also use .split('_')[0] with the map method similar to Dmitry's answer... This gives you the first string of the split array, split at the underscore...

const nullValue = {
  collection: [{
      name: "test_er"
    },
    {
      name: "test_bx"
    },
    {
      name: "fred"
    },
    {
      name: "test_er"
    }
  ]
};

nullValue.collection = [ ...nullValue.collection.map( names => ({ name: names.name.split('_')[0], })),]
console.log(nullValue)

If you want to keep the original array of objects...

const nullValue = {
  collection: [{
      name: "test_er"
    },
    {
      name: "test_bx"
    },
    {
      name: "fred"
    },
    {
      name: "test_er"
    }
  ]
};
const newArr = { collection : 
[ ...nullValue.collection.map( names => 
  ({ name: names.name.split('_')[0], })),
]}

console.log('newArr = ', newArr)
console.log('nullValue = ', nullValue)

about 4 years ago · Juan Pablo Isaza Relatório

0

const nullValue = {
    collection: [
     {
     name: "test_er"
},
    {
     name: "test_bx"
},
    {
     name: "fred"
},
    {
     name: "test_er"
}
    ]
};

nullValue.collection = nullValue.collection.map(i => i.name.replace(/_.*$/, ''))
console.log(nullValue)

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