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

121
Vistas
Javascript array chunk with some rules

need your help. I have an array like;

const arr = 
  [ { item: { 0: 'asd' },                                     que: '1.question asdsad ... '    } 
  , { item: { 0: 'asd' },                                     que: '2.question asdsad ... '    } 
  , { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... '    } 
  , { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' },                 que: 'Question asdsad ... '      } 
  , { item: { 0: 'asd' },                                     que: '4.question asdsad ... '    } 
  , { item: { 0: 'asd' },                                     que: '5.sdd asdsad ... '         } 
  , { item: { 0: 'bbb', 1: 'bbb' },                           que: 'Dsad question asdsad ... ' } 
  ]

As you see each of them has different datas. I want to create new array like chunk array. But it has to be;

**Maximum array length  = 2
**Order must be same 
**if array item length bigger than 3 or equal, it creates self array.
**if array item que does not begin with number, it create self array.

Expected output is;

const expArr = 
  [ [ { item: { 0: 'asd' },                                     que: '1.question asdsad ... '    } 
    , { item: { 0: 'asd' },                                     que: '2.question asdsad ... '    } 
    ] 
  , [ { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... '    } ] 
  , [ { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' },                 que: 'Question asdsad ... '      } ] 
  , [ { item: { 0: 'asd' },                                     que: '4.question asdsad ... '    } 
    , { item: { 0: 'asd' },                                     que: '5.sdd asdsad ... '         } 
    ] 
  , [ { item: { 0: 'bbb', 1: 'bbb' },                           que: 'Dsad question asdsad ... ' } ] 
  ] 

I tried to chunk method of splitting array.

arr.reduce((prev, next)  => {
    if(Object.keys(next.item).length >= 3) {
        // Can be what
    }
    if(isNaN(next.que.split('')[0])) {
        // Can be what
    }
    
})

Above function still give me two sized array, not rule applies

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

0

Basically you can loop each object and within an if statement figure out if it the object is added to a new array or if it is added to the existing array. It is easier to see in this snippet:

const arr = 
  [ { item: { 0: 'asd' },                                     que: '1.question asdsad ... '    } 
  , { item: { 0: 'asd' },                                     que: '2.question asdsad ... '    } 
  , { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... '    } 
  , { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' },                 que: 'Question asdsad ... '      } 
  , { item: { 0: 'asd' },                                     que: '4.question asdsad ... '    } 
  , { item: { 0: 'asd' },                                     que: '5.sdd asdsad ... '         } 
  , { item: { 0: 'bbb', 1: 'bbb' },                           que: 'Dsad question asdsad ... ' } 
  ];
  
  let arNew = [];
  let arTmp = [];
  let sCurItemO = '';
  for(const obj of arr){
    if(obj.item['0'] != sCurItemO || arTmp.length == 2 || Object.keys(obj.item).length > 2 || isNaN(obj.que.substring(0, 1))){
      if(arTmp.length)
        arNew.push(arTmp)
      arTmp = [obj]
    }
    else
        arTmp.push(obj);
    sCurItemO = obj.item['0'];
  }
  arNew.push(arTmp);
  
  console.log(arNew)

about 4 years ago · Juan Pablo Isaza Denunciar

0

I see that this way...

const arr = 
  [ { item: { 0: 'asd' },                                     que: '1.question asdsad ... '    } 
  , { item: { 0: 'asd' },                                     que: '2.question asdsad ... '    } 
  , { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... '    } 
  , { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' },                 que: 'Question asdsad ... '      } 
  , { item: { 0: 'asd' },                                     que: '4.question asdsad ... '    } 
  , { item: { 0: 'asd' },                                     que: '5.sdd asdsad ... '         } 
  , { item: { 0: 'bbb', 1: 'bbb' },                           que: 'Dsad question asdsad ... ' } 
  ]


const maxSize = 2
const itemMax = 3
const result  = []
let lastArr   = null

for (let row of arr)
  {
  let len = Object.keys(row.item).length 

  if ((len >= itemMax) || isNaN(row.que.charAt(0)))
    {
    result.push([row])
    lastArr = null
    }
  else
    {
    if (!lastArr || lastArr.length >= maxSize)
      {
      lastArr = new Array()
      result.push(lastArr)
      }
    lastArr.push(row)
    }
  }

console.log( result )
.as-console-wrapper { max-height: 100% !important; top: 0 }

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