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

97
Vistas
Modifying array in JavaScript using array method

let fruits = ['apple' , 'cherry', 'grape', undefined, 'lemon' ,'melon'];

i have those array of fruits. i want to replace undefined with another fruit, and return all the array with new fruit included. how to do that using array method in javascript i do need help

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You use .splice to replace undefined with new fruit. But just note that it's also possible to achieve it with .map and .filter

let arr = ['apple' , 'cherry', 'grape', undefined, 'lemon' ,'melon'];
 
let index = arr.indexOf(undefined)
arr.splice(index, 1, 'new fruit')

console.log(arr)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Javascript splice method

let fruits = ['apple' , 'cherry', 'grape', undefined, 'lemon' ,'melon'];
fruits.splice(fruits.indexOf(undefined),1,"newFruit")
console.log(fruits)

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is a basic example of what loops are used for. You can use for, or forEach.

let fruits = ['apple' , 'cherry', 'grape', undefined, 'lemon' ,'melon'];


fruits.forEach((fruit,index) => {
  if(fruit == undefined) {
    fruits[index] = 'Some other fruit'
   }
})

console.log(fruits)

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