Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!