The chunk and slice of Array

Posted by sandip123 on Sun, 05 Jan 2020 02:56:53 +0100

1, lodash version: 4.16.1

2, Function.

1,chunk

1) Meaning: unpack an array into multiple size arrays, and then make multiple arrays into a new array.

2) example.

const _ = require('lodash');

console.log(_.chunk([1, 2, 3, 4, 5, 6], 4)); // Output: [[1, 2, 3, 4], [5, 6]]

console.log(_.chunk([1, 2, 3, 4, 5, 6], 2)); // Output: [[1, 2], [3, 4], [5, 6]]

3) Source code interpretation.

(1) First calculate the size. If size is less than 0, the value of size is 0. Otherwise, size takes its own value.

(2) The length of the array is then calculated. If the array is empty, the length is 0, otherwise it is the length of the array.

(3) If the length does not exist or the size is 0, an empty array is returned directly. The specific source code is as follows.

size = Math.max(size, 0)



const length = array == null ? 0 : array.length



if (!length || size < 1) {

  return []

}

(4) Generate an array with a length greater than or equal to length/size and an integer close to length/size. The specific source code is as follows.

let index = 0

let resIndex = 0

const result = new Array(Math.ceil(length / size))

(5) Then loop, intercept the array and insert it into result, and finally return result. The specific source code is as follows.

while (index < length) {

   result[resIndex++] = slice(array, index, (index += size))

}

return result

2,slice

1) Meaning: intercept array. Intercepts a new array containing the start to end positions of the start position. The default value of end is the length of the array, and the default value of start is 0.

2) Format: _.slice(array, [start=0], [end = array.length])

3) example:

const _ = require('lodash');


console.log(_.slice([1, 2, 3, 4, 5], 3)) // Output: [4, 5]

console.log(_.slice([1, 2, 3, 4, 5], 2)) // Output: [3, 4, 5]

4) Source code interpretation.

let length = array == null ? 0 : array.length //Calculates the value of length. If the array is empty, the length is 0, otherwise it is the length of the array.

if (!length) { //Null array if length is 0

    return []

}

start = start == null ? 0 : start // If start is empty, this start is 0, otherwise it is start itself.

end = end === undefined ? length : end // If end does not exist, end is the length of the array; otherwise, it is the end value itself.



if (start < 0) { // If start is less than 0, the absolute value of start is determined. If the absolute value of start is greater than the length of the array, start is set to 0. If the absolute value of start is less than the length of the array, start is set to the value of length + start.

    start = -start > length ? 0 : (length + start) 

}

end = end > length ? length : end // Calculates the value of end. If end is greater than length, the value of end is length, otherwise end.

if (end < 0) { // If end is less than 0, the value of length+end is calculated and assigned to end. The reason why we don't judge whether - end is greater than length here is that end is the end value, not the start value, which is unnecessary.

    end += length

}



length = start > end ? 0 : ((end - start) >>> 0) // Judge whether start is greater than end. If it is greater than end, the length value is 0. Otherwise, the end - start value is calculated.

start >>>= 0



let index = -1

const result = new Array(length) // Generate an array of length.

while (++index < length) {

   result[index] = array[index + start] // Take out the specific elements in the array and insert them into the result.

}

return result

 

Topics: less