Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

53
Vistas
How can I modify every other element in an array using JS?

So the data is like this:

let array = ["Yes", "Yes", "Yes", "Yes"];

Expected Result:

array = ["Yes", "", "Yes", ""];

I've seen some answers on Stackoverflow, but I haven't found one that shows how to modify every other element. Most show how to filter, like this one:

let x = arrar.filter((element, index) => {
  return index % 2 === 0;
})

Appreciate your help!

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As Emiel pointed out in the comments, you don't want to use .filter() here as that would reduce your original array. Instead use .map():

let array = ["Yes", "Yes", "Yes", "Yes"];

let x = array.map((element, index) => {
  return (index % 2 === 0) ? element : '';
})

console.log(x)

7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.