_.chunk(array, [size=1])
Creates an array of elements split into groups the length of size.
If array can't be split evenly, the final chunk will be the remaining elements.
Since
3.0.0
Arguments
-
array (Array): The array to process.
-
[size=1] (number): The length of each chunk
Returns
(Array): Returns the new array of chunks.
Example
_.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]
_.chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]
마인드맵
_. chunk 메서드를 사용하면 매개변수 값만큼 배열의 size별로 앞에서 부터 묶어 새로운 이중배열을 반환합니다.
- 새로운 이중 배열의 반환
- 매개변수 size로 숫자를 나누듯 배열을 나누고 마지막 배열은 항상 size 보다 length가 같거나 적은 배열이 반환 될 것으로 예상된다.
- 매개변수 size 와 마지막 반환배열의 length가 같다면 나머지 없이 모두 그룹핑 된 것, 아닌 경우 마지막배열이 나머지그룹의 배열.
- JS활용 서버에 대량으로 처리해야할 작업의 예약리스트, 한번에 처리할 작업을 배열로 지정된 숫자만큼 분할해서 처리하고자 할때?
- 10만건의 rowData를 저장할때 한번에 10만건을 던지지 않고, 요청할 작업 수를 [ 지정된 수 ] 로 나눠서 배열로 반환 받고 지정된 수만큼의 길이인 배열로 나눠서 요청할 수 있을 것 같다.
- 반환되는 새로운 이중배열의 length 기준으로도 조건을 맞춰서 중간단계에서 일정 수준으로 작업량 모아서 작업요청 할수도 있을 것 같다.
Source
/**
* Creates an array of elements split into groups the length of `size`.
* If `array` can't be split evenly, the final chunk will be the remaining
* elements.
*
* @static
* @memberOf _
* @since 3.0.0
* @category Array
* @param {Array} array The array to process.
* @param {number} [size=1] The length of each chunk
* @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
* @returns {Array} Returns the new array of chunks.
* @example
*
* _.chunk(['a', 'b', 'c', 'd'], 2);
* // => [['a', 'b'], ['c', 'd']]
*
* _.chunk(['a', 'b', 'c', 'd'], 3);
* // => [['a', 'b', 'c'], ['d']]
*/
function chunk(array, size, guard) {
if ((guard ? isIterateeCall(array, size, guard) : size === undefined)) {
size = 1;
} else {
size = nativeMax(toInteger(size), 0);
}
var length = array == null ? 0 : array.length;
if (!length || size < 1) {
return [];
}
var index = 0,
resIndex = 0,
result = Array(nativeCeil(length / size));
while (index < length) {
result[resIndex++] = baseSlice(array, index, (index += size));
}
return result;
}
var nativeCeil = Math.ceil,
nativeMax = Math.max,
[1] Math.ceil() 함수는 주어진 숫자보다 크거나 같은 숫자 중 가장 작은 숫자를 integer 로 반환합니다.
Math.ceil(.95); // 1
Math.ceil(4); // 4
Math.ceil(7.004); // 8
Math.ceil(-0.95); // -0
Math.ceil(-4); // -4
Math.ceil(-7.004); // -7
[2] Math.max()함수는 0이상의 숫자 중 가장 큰 숫자를 반환합니다.
Math.max(10, 20); // 20
Math.max(-10, -20); // -10
Math.max(-10, 20); // 20
/**
* Checks if the given arguments are from an iteratee call.
*
* @private
* @param {*} value The potential iteratee value argument.
* @param {*} index The potential iteratee index or key argument.
* @param {*} object The potential iteratee object argument.
* @returns {boolean} Returns `true` if the arguments are from an iteratee call,
* else `false`.
*/
function isIterateeCall(value, index, object) {
if (!isObject(object)) {
return false;
}
var type = typeof index;
if (type == 'number'
? (isArrayLike(object) && isIndex(index, object.length))
: (type == 'string' && index in object)
) {
return eq(object[index], value);
}
return false;
}
/**
* The base implementation of `_.slice` without an iteratee call guard.
*
* @private
* @param {Array} array The array to slice.
* @param {number} [start=0] The start position.
* @param {number} [end=array.length] The end position.
* @returns {Array} Returns the slice of `array`.
*/
function baseSlice(array, start, end) {
var index = -1,
length = array.length;
if (start < 0) {
start = -start > length ? 0 : (length + start);
}
end = end > length ? length : end;
if (end < 0) {
end += length;
}
length = start > end ? 0 : ((end - start) >>> 0);
start >>>= 0;
var result = Array(length);
while (++index < length) {
result[index] = array[index + start];
}
return result;
}
'development > myLib' 카테고리의 다른 글
[Lodash][Array] _.concat (0) | 2020.05.03 |
---|---|
[Lodash][Array] _.compact (0) | 2020.05.02 |
라이브러리 분석 시작하기 (0) | 2020.05.01 |