Home > front end >  Javascript Chunk Array according to subArray length
Javascript Chunk Array according to subArray length

Time:11-18

I have a problem about chunking subarrays. As you see below, there are many òbject inside detail. I want to split them if detail[i].length detail[i 1].length >= 11 chunk array with 2, else chunk 3.

const isQuestions = 
  [ { question: ['ques 1?'], detail: [ {a:1}, {a:2}, {a:3}, {a:4}, {a:5}, {a:6} ] } 
  , { question: ['ques 2'],  detail: [ {b:1}, {b:2}, {b:3}, {b:4}, {b:5}, {b:6}, {b:7}, {b:8} ] } 
  , { question: ['ques 3?'], detail: [ {c:1}, {c:2}, {c:3}, {c:4}, {c:5}, {c:6}, {c:7} ] } 
  , { question: ['ques 4'],  detail: [ {d:1}, {d:2}, {d:3}, {d:4} ] } 
  , { question: ['ques 5'],  detail: [ {e:1}, {e:2}, {e:3}, {e:4} ] } 
  , { question: ['ques 6'],  detail: [ {f:1}, {f:2}, {f:3}, {f:4} ] }
  , { question: ['ques 7'],  detail: [ {g:1}, {g:2}, {g:3}, {g:4} ] } 
  ]

Expected array be like;

[ [ { question: ['ques 1?'], detail: [ {a:1}, {a:2}, {a:3}, {a:4}, {a:5}, {a:6} ] }
  , { question: ['ques 2'],  detail: [ {b:1}, {b:2}, {b:3}, {b:4}, {b:5}, {b:6}, {b:7}, {b:8} ] } 
  ]
, [ { question: ['ques 3?'], detail: [ {c:1}, {c:2}, {c:3}, {c:4}, {c:5}, {c:6}, {c:7} ] }
  , { question: ['ques 4'],  detail: [ {d:1}, {d:2}, {d:3}, {d:4} ] } 
  ]
, [ { question: ['ques 5'],  detail: [ {e:1}, {e:2}, {e:3}, {e:4} ] }
  , { question: ['ques 6'],  detail: [ {f:1}, {f:2}, {f:3}, {f:4} ] }
  , { question: ['ques 7'],  detail: [ {g:1}, {g:2}, {g:3}, {g:4} ] } 
] ] 

As you see above, the sum of expected array's last part is 12. But It does not matter, the important thing is arr[i] arr[i 1] length.

I did a function like inside map function. Because I have multiple array like this.

function isApsChunk(rawArray, size) {
    var returnedArray = [];
    for (var i = 0; i < rawArray.length; i  = size) {
        if (rawArray[i   1])
            if (rawArray[i].detail.length   rawArray[i   1].detail.length >= 11) {
                returnedArray.push(rawArray.slice(i, i   2));
            } else {
                returnedArray.push(rawArray.slice(i, i   size));
            }
    }
    return [returnedArray];
}
console.log(isApsChunk(isQuestions, 3))

But the problem is that function takes array with the length 7, gives me 5.

CodePudding user response:

this way:
with the help of Array.reduce()

in this code the argument r (acronym of result [accumulator in doc MDN]) is initialized as follows:
r = {resp: [], blk: null, len: 0, count: 0}

r.resp becomes the final accumulator
r.blk points to the sub array to fill
r.len = size of the accumulation of arr detail
r.count = r.blk.length, linearity here has 3 by the size argument of the function

{[i 1]: next} allows to point to the element i 1 and name it next, they guess undefined when it is outside the array (when we are on the last element, the next one does not exist and !!next become false

!!next is equivalent to Boolean(next)

const isQuestions = 
  [ { question: ['ques 1?'], detail: [ {a:1}, {a:2}, {a:3}, {a:4}, {a:5}, {a:6} ] } 
  , { question: ['ques 2'],  detail: [ {b:1}, {b:2}, {b:3}, {b:4}, {b:5}, {b:6}, {b:7}, {b:8} ] } 
  , { question: ['ques 3?'], detail: [ {c:1}, {c:2}, {c:3}, {c:4}, {c:5}, {c:6}, {c:7} ] } 
  , { question: ['ques 4'],  detail: [ {d:1}, {d:2}, {d:3}, {d:4} ] } 
  , { question: ['ques 5'],  detail: [ {e:1}, {e:2}, {e:3}, {e:4} ] } 
  , { question: ['ques 6'],  detail: [ {f:1}, {f:2}, {f:3}, {f:4} ] }
  , { question: ['ques 7'],  detail: [ {g:1}, {g:2}, {g:3}, {g:4} ] } 
  ]  

const isApsChunk = (rawArray, size) => 
  rawArray.reduce((r,elm,i,{[i 1]:next}) =>
  {
  if (r.len===0 || r.len >= 11 || r.count >= size)
    {
    r.len   = 0
    r.count = 0
    r.blk   = []
    r.resp.push(r.blk )
    }
  r.len  = elm.detail.length
  r.count  
  r.blk.push( elm )

  return (!!next)?r:r.resp
  },{ resp:[], blk:null, len:0, count:0 } )
  
console.log(  isApsChunk(isQuestions, 3) )
.as-console-wrapper { max-height: 100% !important; top: 0 }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related