0

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.

1
  • @MisterJojo it has something like that you mentioned. But it is not important, just you think Object.length Commented Nov 17, 2021 at 15:06

1 Answer 1

1

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 }

Sign up to request clarification or add additional context in comments.

2 Comments

Can you add some comment blocks for more understanding, please?
@apsisx I added some explanations, need more ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.