简体   繁体   中英

JavaScript Array Push Missing Values

This function will take an array and chunk it into separate arrays, create an offset at the beginning and wrap that in another array. The problem I have is not all of the numbers from the original array (arr1) are included in the chunks. You can see the output in the link I have provided below. It misses out numbers 5, 13, 21, 29 and 30. Can anyone explain why this happens?

function chunkifyArray(input, chunkSize, offset) {

    const output = [];

    let tmp = offset ? new Array(offset).fill('') : [];

    for(var i = 0; i < input.length; i++){
        if (tmp.length < chunkSize) {
            tmp.push(input[i]);
        } else {
            output.push(tmp);
            tmp = [];     
        }
    }

    return output;
}

var arr1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'];
console.log(chunkifyArray(arr1, 7, 3));

https://jsbin.com/zucaguvoti/edit?js,console

Because you never push those values - you empty tmp insead… Try this:

for(var i = 0; i < input.length; i++){
    tmp.push(input[i]);
    if (tmp.length == chunkSize) {
        output.push(tmp);
        tmp = [];     
    }
}

Edited to ensure the last chunk is pushed onto output...

I changed little bit your code to get to the desired result. You can use the % operator as well as remember to add the last portion at the end of the for loop:

 var data = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30']; function chunkifyArray(input, chunkSize, offset=0) { let output = [], tmp = []; input = new Array(offset).fill('').concat(input); for (var i = 0; i < input.length; i++) { tmp.push(input[i]) if (i && i%chunkSize == 0) { output.push(tmp); tmp = [] } } output.push(tmp); // add the remaining ones return output; } console.log(chunkifyArray(data, 7, 3)); 

You could chunk an array with ES6 and Array.reduce :

 const data = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'] const chunkBy = (arr, by=2, offset=0) => new Array(offset).fill('').concat(arr) .reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), []) console.log(chunkBy(data, 7, 3)) 

When not in compact form this would look like:

 const data = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'] const chunkBy = (arr, by=2, offset=0) => { arr = new Array(offset).fill('').concat(arr) return arr.reduce((r,c,i) => { if(i%by==0) r.push([c]) else r[r.length-1] = [...r[r.length-1], c] return r }, []) } console.log(chunkBy(data, 7, 3)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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