Is there an elegant, functional way to turn this array:
[ 1, 5, 9, 21 ]
in that
[ [1, 5], [5, 9], [9, 21] ]
I know that I can forEacharray and collect values to create a new array. Is there an elegant way to do this in _.lodashwithout using forEach?
forEach
_.lodash
You can match the spliced array and check the index. If it is not equal to zero, take the predecessor; otherwise, take the first element of the original array.
var array = [1, 5, 9, 21], result = array.slice(1).map((a, i, aa) => [i ? aa[i - 1] : array[0], a]); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
An even shorter version suggested by Bergi :
var array = [1, 5, 9, 21], result = array.slice(1).map((a, i) => [array[i], a]); console.log(result);
map :
map
const arr = [ 1, 5, 9, 21 ]; const grouped = arr.map((el, i) => [el, arr[i+1]]).slice(0, -1); console.log(grouped);
array.reduce. : , , .
array.reduce
const arr = [ 1, 5, 9, 21 ]; const chunked = arr.reduce((p, c, i, a) => i === 0 ? p : (p.push([c, a[i-1]]), p), []); console.log(chunked);
:
const arr = [1, 5, 9, 21]; const chunked = arr.reduce(function(previous, current, index, array) { if(index === 0){ return previous; } else { previous.push([ current, array[index - 1]]); return previous; } }, []); console.log(chunked);
ramda, aperture - , .
, ramda:
R.aperture(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [2, 3], [3, 4], [4, 5]] R.aperture(3, [1, 2, 3, 4, 5]); //=> [[1, 2, 3], [2, 3, 4], [3, 4, 5]] R.aperture(7, [1, 2, 3, 4, 5]); //=> []
, , , (arr[i + 1] arr[i - 1]).
arr[i + 1]
arr[i - 1]
, reduce , , .
reduce
part
n
shift
slice
length
const partition = partitionSize => arr => { const part = []; return arr.reduce((parts, x) => { part.push(x); if (part.length === partitionSize) { parts.push(part.slice()); part.shift(); } return parts; }, []); }; const makePairs = partition(2); const makeTrios = partition(3); const pairs = makePairs([1,2,3,4,5,6]); const trios = makeTrios([1,2,3,4,5,6]); console.log("partition(2)", JSON.stringify(pairs)); console.log("partition(3)", JSON.stringify(trios));
.reduce() ;
.reduce()
var arr = [ 1, 5, 9, 21 ], pairs = arr.reduce((p,c,i) => i == 1 ? [[p,c]] : p.concat([[p[p.length-1][1],c]])); console.log(pairs);
, , , , 1 .
() [ 1, 5, 9, 21, 33 ] [ [1, 9], [5, 21], [9, 33] ], , 2.
[ 1, 5, 9, 21, 33 ]
[ [1, 9], [5, 21], [9, 33] ]
1, .
slide, ,
slide
slide , . 2 1
// take :: (Int, [a]) -> [a] const take = (n, xs) => xs.slice(0, n) // drop :: (Int, [a]) -> [a] const drop = (n, xs) => xs.slice(n) // slice :: (Int, Int, [a]) -> [[a]] const slide = (m, n, xs) => xs.length > m ? [take(m, xs), ...slide(m, n, drop(n, xs))] : [xs] const arr = [0,1,2,3,4,5,6] // log helper improves readability of output in stack snippet const log = x => console.log(JSON.stringify(x)) log(slide(1, 1, arr)) // [[0],[1],[2],[3],[4],[5],[6]] log(slide(1, 2, arr)) // [[0],[2],[4],[6]] log(slide(2, 1, arr)) // [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6]] log(slide(2, 2, arr)) // [[0,1],[2,3],[4,5],[6]] log(slide(3, 1, arr)) // [[0,1,2],[1,2,3],[2,3,4],[3,4,5],[4,5,6]] log(slide(3, 2, arr)) // [[0,1,2],[2,3,4],[4,5,6]] log(slide(3, 3, arr)) // [[0,1,2],[3,4,5],[6]]
If for some reason you did not want to slideinclude partial fragments (less slices m), we could edit them as such
m
// slice :: (Int, Int, [a]) -> [[a]] const slide = (m, n, xs) => xs.length > m ? [take(m, xs), ...slide(m, n, drop(n, xs))] : [] // <- return [] instead of [xs] log(slide(2, 2, arr)) // now prints: [[0,1],[2,3],[4,5]] // instead of: [[0,1],[2,3],[4,5],[6]]
Source: https://habr.com/ru/post/1677858/More articles:Error using Auth0 Quick Launch - angularWhy is there no O_SEARCH flag for an open function in Open Open? - linuxКак уменьшить шум с помощью SOX? - audio-recordingСоздание естественного языка - как выйти за рамки шаблонов - textWhy does a floating div make the next div take up whole space? - htmlIterate an array as a pair (current, next) in JavaScript - javascriptDo I need a separate object for each @RequestBody in Spring Boot - javaSkip child to retrieve parent - JPA - javaDownload Java JDK via Script - linuxHow to join a pandas data framework so that a marine frame or violin can use a column as a hue? - pythonAll Articles