繁体   English   中英

Javascript 根据子数组长度的块数组

[英]Javascript Chunk Array according to subArray length

我有一个关于分块子数组的问题。 正如您在下面看到的,有很多òbject inside detail if detail[i].length + detail[i+1].length >= 11块数组与 2,我想拆分它们,否则为块 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} ] } 
  ]

预期的数组是这样的;

[ [ { 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} ] } 
] ] 

正如您在上面看到的,预期数组最后一部分的总和是 12。但没关系,重要的是arr[i] + arr[i+1] length

我在 map function 内部做了一个 function。 因为我有多个这样的数组。

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))

但问题是 function 采用长度为 7 的数组,给我 5。

这边走:
Array.reduce()的帮助下

在此代码中,参数 r(result [accumulator in doc MDN] 的首字母缩写)初始化如下:
r = {resp: [], blk: null, len: 0, count: 0}

r.resp成为最终累加器
r.blk指向要填充的子数组
r.len = size
r.count = r.blk.length ,这里的线性度有 3 由 function 的size参数

{[i + 1]: next}允许指向元素i + 1并将其命名为next ,当它在数组之外时,他们猜测 undefined (当我们在最后一个元素上时,下一个不存在并且!!next变成假的

!!next等价于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 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM