如何用lodash转换数组?

时间:2016-04-28 12:51:03

标签: javascript lodash

如何用lodash将第一个数组转换为第二个?

来自:

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

致:

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

1 个答案:

答案 0 :(得分:2)

你可以这样做:



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>
&#13;
&#13;
&#13;