Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
merge-arrays-alternatingly-from-tail
node v8.17.0
version:
1.0.0
endpoint
share
tweet
const flow = require('lodash/fp/flow'); const map = require('lodash/fp/map'); const reduce = require('lodash/fp/reduce'); const flatten = require('lodash/fp/flatten'); const reverse = require('lodash/fp/reverse'); const alphabets = ['a', 'b', 'c', 'd', 'e']; const numbers = [1, 2, 3]; const alphabets2 = ['f']; const numbers2 = [4]; const mergeArraysAlternatingly = (former, latter) => { console.log(`former array : ${former}`); console.log(`latter array : ${latter}`); return flow( map(array => reverse(array)), reduce((result, array) => { map.convert({cap: false})((item, i) => { (result[i] = result[i] || []).push(item); return result[i]; })(array); return result; }, []), flatten, reverse )([latter, former]); } console.log('result : ' + mergeArraysAlternatingly(alphabets, numbers));
Created from: https://npm.runkit.com/
Loading…
no comments
sign in
to comment