簡體   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