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

104
Vistas
How to triplicate specific items in an array for javascript?

Is there any way to only triplicate certain elements in an array? I want to triplicate the "3" in the array only. For instance: const deck = [1, 3, 9, 3, 7]; should become [1, 3, 3, 3, 9, 3, 3, 3, 7]

I have tried the method below, deck is the random array:

var i;
  for (let i=0;i<deck.length;i++){
    if (deck[i]==3){
      return deck.flatMap(x=>[x,x,x]);
    }else return deck[i];
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could write a function that uses flatMap() to map over the array and triplicate the specific values.

const deck = [1, 3, 9, 3, 7];

const triplicate = (number, arr) => {
  return arr.flatMap(x => x === number ? [x, x, x]: x);
}

console.log(triplicate(3, deck));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Using Array#flatMap:

const triplicate = (arr = [], number) => 
  arr.flatMap(n => n === number ? [n, n, n] : n);

console.log( triplicate([1, 3, 9, 3, 7], 3) );

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