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

134
Vistas
Repeat the array element two times in JavaScript

I was writing a code in JavaScript to just repeat the array element two times. Here is my code.

var arr = [1,2];
for(var i=0;i!=arr.length;i++)
{
  arr.push(arr[i]);
}

for(var i=0;i!=arr.length;i++)
{
  console.log(arr[i]);
}

For example for arr =[1,2 ] output should be [1,2,1,2];

But I am getting error as:FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory

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

0

arr.length grows while you're pushing new elements into the array. You can store the length in a separate variable:

const arr = [1, 2];
for(let i = 0, length = arr.length; i != length; ++i)
{
  arr.push(arr[i]);
}

for(let i = 0; i != arr.length; ++i)
{
  console.log(arr[i]);
}

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