Get consecutive numbers in an array

now you want to split an array according to the specified size.
chunk (arr, 10)
divide the array into a sum less than or equal to 10. Please consult the gods

.
let arr = [15,1,3,4,3,4,10,7,19]


function chunk(arr, sum) {

}
// [[15], [1, 3, 4], [3, 4], [10], [7], [19]]
Aug.16,2021

    var arr = [15,1,3,4,3,4,10,7,19];
    
    function chunk(arr, critical) {
        var result = [];
        var sum = 0;
        var temp = [];
        arr.forEach(function(item) {
            if(!temp.length) {
                temp.push(item);
                sum = item;
            } else {
                if(sum > critical - item) {
                    result.push(temp);
                    sum = item;
                    temp = [item];
                } else {
                    sum += item;
                    temp.push(item);
                }
            }
        });
        result.push(temp);
        return result;
    }
    var result = chunk(arr, 10);
    console.log(result);

let arr = [15, 1, 3, 4, 3, 4, 10, 7, 19];
let chunk = arr.reduce((arr, num) => {
  const n = arr[arr.length - 1];
  if (n.length && n.reduce((a, b) => a + b, num) > 10) {
    arr.push([num]);
  } else {
    n.push(num);
  }
  return arr;
}, [[]]);
console.log(chunk);

    var arr = [15,1,3,4,3,4,10,7,19],         
        o = [0],
        newArr = [];
    arr.reduce(function (p, v, index) {
        p + v > 10 ? o.push(index) : false;
        return p + v > 10 ? v : p + v;
    });
    for(var i = 0; i < o.length; iPP) {
        newArr.push(arr.slice(o[i], o[i+1]))
    }
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb4b56-24f7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb4b56-24f7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?