简体   繁体   中英

how to convert array with lodash?

How can i convert first array to second with lodash ?

From :

var data = [
  {'column':[1,1,1,1,1]},
  {'column':[2,2,2,2,2]},
  {'column':[3,3,3,3,3]},
]

To :

var data = [
  {'column':[1,2,3]},
  {'column':[1,2,3]},
  {'column':[1,2,3]},
  {'column':[1,2,3]},
  {'column':[1,2,3]},
]

You can do it like this:

 var data = [ {'column':[1,1,1,1,1]}, {'column':[2,2,2,2,2]}, {'column':[3,3,3,3,3]}, ]; var newData = _(data) .map('column') .unzip() .map(function(col) { return {'column': col}; }) .value(); console.log(newData); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.min.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM