How to use the unzip function from lodash
Find comprehensive JavaScript lodash.unzip code examples handpicked from public code repositorys.
lodash.unzip is a function that takes an array of arrays and groups the values by their index position, returning a new array of arrays where each sub-array contains the values from the original arrays at that index position.
419 420 421 422 423 424 425 426 427 428
module.exports.uniqueId = _.uniqueId; module.exports.unset = _.unset; module.exports.unsplat = _.unsplat; module.exports.unsplatl = _.unsplatl; module.exports.unsplatr = _.unsplatr; module.exports.unzip = _.unzip; module.exports.unzipWith = _.unzipWith; module.exports.update = _.update; module.exports.updatePath = _.updatePath; module.exports.updateWith = _.updateWith;
+ 92 other calls in file
GitHub: mdmarufsarker/lodash
168 169 170 171 172 173 174 175 176 177 178 179 180
console.log(uniqBy); // => [2.1, 1.2] const uniqWith = _.uniqWith([2.1, 1.2, 2.3], (a, b) => Math.floor(a) === Math.floor(b)); console.log(uniqWith); // => [2.1, 1.2] const unzip = _.unzip([['a', 1, true], ['b', 2, false]]); console.log(unzip); // => [['a', 'b'], [1, 2], [true, false]] const unzipWith = _.unzipWith([[1, 10, 100], [2, 20, 200]], (...arrays) => _.sum(arrays)); console.log(unzipWith); // => [3, 30, 300]
+ 15 other calls in file
How does lodash.unzip work?
Sure! lodash.unzip takes an array of arrays as its input. It then creates a new array with the same number of sub-arrays as the length of the first sub-array in the input array. It then iterates over each sub-array in the input array and pushes the value at the current index of that sub-array into the corresponding sub-array in the output array. For example, if the input array is [[1, 4], [2, 5], [3, 6]], the output array would be [[1, 2, 3], [4, 5, 6]]. This is because the first sub-array in the input array is [1, 4], so the first value in the output array will be an array containing 1, 2, and 3, which are the first values from each sub-array in the input array. The second sub-array in the input array is [2, 5], so the second value in the output array will be an array containing 4, 5, and 6, which are the second values from each sub-array in the input array.
GitHub: Hupeng7/es6demo
484 485 486 487 488 489 490 491 492 493 494 495
//_.unzip(array) let zipped = _.zip(['a', 'b'], [1, 2], [true, false]); console.log('zipped--->', zipped); //zipped---> [ [ 'a', 1, true ], [ 'b', 2, false ] ] let unzip1 = _.unzip(zipped); console.log('unzip1--->', unzip1); //unzip1---> [ [ 'a', 'b' ], [ 1, 2 ], [ true, false ] ] //_.unzipWith(array,[iteratee=_.indentity])
Ai Example
1 2 3 4 5 6 7 8 9 10 11
const _ = require("lodash"); const arr = [ [1, 4], [2, 5], [3, 6], ]; const unzipped = _.unzip(arr); console.log(unzipped); // Output: [[1, 2, 3], [4, 5, 6]]
In this example, we first import lodash and assign the unzip function to the _ variable. We then define an array arr containing three sub-arrays, each with two values. We call _.unzip(arr) to unzip the array, which returns a new array with two sub-arrays, each containing the values from arr at the same index position. Finally, we log the unzipped array to the console, which outputs [[1, 2, 3], [4, 5, 6]].
lodash.get is the most popular function in lodash (7670 examples)