简体   繁体   中英

How do I split a number into chunks of max 100 in JavaScript?

I have a number (let's say 525). I would like to take this number and split it into an array of chunks with a max of 100 each value. If I took 525 and split it into an array, it would look like:

[
    100,
    100,
    100,
    100,
    100,
    25
]

Here's what I've tried so far:

var number = 525;
var array = [];
while (number > 0) {
    number = number - 100;
    array.push(Math.min(number, 100));
}

That doesn't get me far. It just returns [ 100, 100, 100, 100, 25, -75 ] . I know that using while isn't the best way to go, but that is what I could think of off the top of my head. Does anyone have any other way that improve my code and be more efficient?

You can figure the number of times that number is divisible by 100 then programmatically create an array of that length:

var number = 525;
var array = new Array(Math.floor(number / 100)).fill(100).concat(number % 100))
// [ 100, 100, 100, 100, 100, 25 ]

You can extend this to chunk by any number:

function chunkBy(number, n) {
  var chunks = Array(Math.floor(number / n)).fill(n);
  var remainder = number % n;

  if (remainder > 0) {
    chunks.append(remainder);
  }
  return chunks;
}

Alternatively, simply push the element before performing your subtraction:

var number = 525;
var array = [];
while (number > 0) {
    array.push(Math.min(number, 100));
    number = number - 100;
}
// [ 100, 100, 100, 100, 100, 25 ]

Using ES6 with arrow functions:

const chunkBy = (n) => number => {
  var chunks = new Array(Math.floor(number / n)).fill(n);
  var remainder = number % n;
  console.log('CHUNKS = ', chunks);
  if (remainder > 0) {
    chunks.push(remainder);
  }

  return chunks;
};

const chunkBy50 = chunkBy(50);
const chunkBy100 = chunkBy(100);
const chunkBy77 = chunkBy(77);
console.log(chunkBy50(500));
// [ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50 ]
console.log(chunkBy100(500));
// [ 100, 100, 100, 100, 100 ]
console.log(chunkBy77(500));
// [ 77, 77, 77, 77, 77, 77, 38 ]

You should move the line number = number - 100; after you push the number, so it will count the 1st 100, and stop before pushing the -75:

 var number = 525; var array = []; while (number > 0) { array.push(Math.min(number, 100)); number = number - 100; // remove number after pushing } console.log(array); 

And a fancy one using Array#from :

 const number = 525; const get100s = (number) => Array.from({ length: Math.ceil(number / 100) }, (_, i) => { const leftover = number - i * 100; return leftover > 100 ? 100 : leftover; }); console.log('525: ', get100s(525)); console.log('500: ', get100s(500)); console.log('10: ', get100s(10)); 

Alternatively, you could use integer division to get the number of occurrences for the "max" divisor and then use the "mod" operation to get the final chunk or remainder.

const number = 525;
const max = 100;

const maxCount = Math.floor(number / max); // 5
const remainder = 525 % max; // 25

The problem here is that you are substracting 100 to the number after adding it to the array.

The code should be like this:

var number = 525;
var array = [];
while (number > 0) {
  array.push(Math.min(number, 100));
  number = number - 100;
}

Apparently I think completely different about this kind of problem. I would have divided the number by the section size and rounded down/truncated to get the number of sections, added that many sections to the final array, and then added the modulus of the section size and the number as the final value.

 var value = 525; function splitValue(val, sectionSize) { var sections = Math.floor(val/sectionSize); var finalValue = val%sectionSize; var splitValues = []; for (var i = 0; i < sections; i++) { splitValues.push(sectionSize); } splitValues.push(finalValue); return splitValues; } var valueArray = splitValue(value, 100); console.log(valueArray); 

You can also use modulus.

let x = 556;
let mod = 100; 
let res = [];
for (let i = 0; i < parseInt(x / mod); i++) res.push(mod);
res.push(x % mod);

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