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

106
Vistas
Array Split in JavaScript

I have a JavaScript array as follows

let tasks =[
     {id:1,level:1},
     {id:1,level:2},
     {id:1,level:3},
     {id:2,level:1},
     {id:2,level:2},
     {id:3,level:1}]

I want to split this array into

let tasks =[
         [{id:1,level:1},
          {id:1,level:2},
          {id:1,level:3}],

         [{id:2,level:1},
          {id:2,level:2}],

         [{id:3,level:1}] ]

How to do this?

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

0

You can do it by using reduce helper, like this:

const tasks =[
     {id:1,level:1},
     {id:1,level:2},
     {id:1,level:3},
     {id:2,level:1},
     {id:2,level:2},
     {id:3,level:1}];

const newTasks = tasks.reduce((acc, data)=> {
    const target = acc.find(subArr => subArr.find(item => item.id == data.id));
    target ? target.push(data) : acc.push([data]);
    return acc;
} , []);

console.log(newTasks)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can write a loop to iterate through this array, push half to a and half to b.

a=[]; b=[];
for(int i=0;i<tasks.length;i++){
 if(i<=tasks.lenght/2){
    a.push(tasks[i]);
  }else{
    b.push(tasks[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