JavaScript : Make an array of value pairs form an array of values
问题 Is there an elegant, functional way to turn this array: [ 1, 5, 9, 21 ] into this [ [1, 5], [5, 9], [9, 21] ] I know I could forEach the array and collect the values to create a new array. Is there an elegant way to do that in _.lodash without using a forEach ? 回答1: You could map a spliced array and check the index. If it is not zero, take the predecessor, otherwise 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] :