Home > OS >  Javascript array chunk with some rules
Javascript array chunk with some rules

Time:10-15

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

CodePudding user response:

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 }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related