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

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

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 Denunciar

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 Denunciar

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 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