Map Array Arrays

Is there a way in lodash to display an array of arrays

I would like to do something similar to preserve the structure of the array.

def double(x) { return x*2 } _([[1,2],[3,4]]).somemethod(double) == [[2,4],[6,8]] 
+13
source share
4 answers

You can make your code much cleaner using the ES2015 arrow functions:

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

Using JS Vanilla:

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

Just _.map twice:

 var array = [[1, 2], [3, 4]]; var doubledArray = _.map(array, function (nested) { return _.map(nested, function (element) { return element * 2; }); }); 

Or without lodash :

 var doubledArray = array.map(function (nested) { return nested.map(function (element) { return element * 2; }); }); 

Also, consider using the es6 arrow functions :

 var doubledArray = array.map(nested => nested.map(element => element * 2)); 
+17
source

It could be a kind of confusion:

 var Coef = Array.apply(null, Array(3)).map(function(){return Array.apply(null, Array(4)).map(function(){return 0})}) 

However, this can be useful if you want to initialize the array in Gas.

0
source
 const deepMap=(input,callback)=>input.map(entry=>entry.map?deepMap(entry,callback):callback(entry)) //test deepMap([1,2,3,[1,2]],x=>x*2) // [1,4,9,[1,4]] 
0
source

Source: https://habr.com/ru/post/1242732/


All Articles