Monday, May 20, 2024
72
rated 0 times [  79] [ 7]  / answers: 1 / hits: 146231  / 8 Years ago, wed, february 10, 2016, 12:00:00

Is there a method in lodash to map over an array of arrays



I would like to do something like this so that it keeps the structure of the array.



def double(x) { return x*2 }

_([[1,2],[3,4]]).somemethod(double) == [[2,4],[6,8]]

More From » underscore.js

 Answers
40

You can make your code much cleaner with ES2015 arrow functions:



var array = [[1, 2], [3, 4]];
var double = x => x * 2;
var doubledArray = _.map( array, subarray => _.map( subarray, double ));


Using vanilla JS:



var array = [[1, 2], [3, 4]];
var double = x => x * 2;
var doubledArray = array.map( subarray => subarray.map( double ));

[#63357] Tuesday, February 9, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eliasf

Total Points: 703
Total Questions: 97
Total Answers: 129

Location: Chad
Member since Tue, Apr 27, 2021
3 Years ago
;