javascript - Pivot data using lodash -
i need convert the init array final array preferably using lodash.
initarray = [ { "date":"2017-08-15", "data":[ { "color":"orange", "count":100 }, { "color":"green", "count":101 } ] }, { "date":"2017-08-14", "data":[ { "color":"orange", "count":102 }, { "color":"green", "count":103 } ] } ] finalarray = [ { "color":"orange", "data":[ 100, 102 ] }, { "color":"green", "data":[ 101, 103 ] } ]
this way seems lodash calls make sense me.
// var _ = require('lodash') initarray = [ { "date":"2017-08-15", "data":[ { "color":"orange", "count":100 }, { "color":"green", "count":101 } ] }, { "date":"2017-08-14", "data":[ { "color":"orange", "count":102 }, { "color":"green", "count":103 } ] } ] result = _(initarray) //.map('data') //.flatten() .flatmap('data') // instead of .map('data').flatten() .groupby('color') .map((item, key) => ({ color: key, count: _.map(item, 'count') })) .value() console.log(result)
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script>
Comments
Post a Comment