Reputation: 379
I have this array:
[ [ '2018-05-28T21:51:00Z',
0.00000858,
0.00000857,
0.00000860,
0.00000855,
12511.81490226 ],
[ '2018-05-28T21:52:00Z',
0.00000853,
0.00000850,
0.00000856,
0.00000847,
12687.20140187 ],
[ '2018-05-28T21:53:00Z',
0.00000848,
0.00000847,
0.00000850,
0.00000846,
12708.9320888 ]
]
And i need to transform it to this:
{
T: [ '2018-05-28T21:51:00Z', '2018-05-28T21:52:00Z', '2018-05-28T21:53:00Z' ],
O: [ 0.00000858, 0.00000853, 0.00000848],
C: [ 0.00000857, 0.00000850, 0.00000847],
H: [ 0.00000860, 0.00000856, 0.00000850],
L: [ 0.00000855, 0.00000847, 0.00000846],
V: [ 12511.81490226, 12687.20140187, 12708.9320888 ]
}
It's predefined places, the first values is always 'T', the second values is always 'O' and so on.
Tried with lodash like this
_.zipObject(['T', 'O', 'H', 'L', 'C'], s.values)
But the results are not the expected. Some advices?
Upvotes: 0
Views: 145
Reputation: 725
You can do it by just using zip and zipobject:
var input = [ [ '2018-05-28T21:51:00Z',
0.00000858,
0.00000857,
0.00000860,
0.00000855,
12511.81490226 ],
[ '2018-05-28T21:52:00Z',
0.00000853,
0.00000850,
0.00000856,
0.00000847,
12687.20140187 ],
[ '2018-05-28T21:53:00Z',
0.00000848,
0.00000847,
0.00000850,
0.00000846,
12708.9320888 ]
];
var output = _.zipObject(['T', 'O', 'H', 'L', 'C'], _.zip.apply(_, input));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
Upvotes: 2
Reputation: 806
You can achieve this by doing so:
const trans = data[0].map((col, i) => data.map(row => row[i]));
_.zipObject(['T', 'O', 'H', 'L', 'C'], trans)
data is your initial array.
Upvotes: 0
Reputation: 371168
No need for a library, you can accomplish this easily with plain Javascript:
const input=[['2018-05-28T21:51:00Z',0.00000858,0.00000857,0.00000860,0.00000855,12511.81490226],['2018-05-28T21:52:00Z',0.00000853,0.00000850,0.00000856,0.00000847,12687.20140187],['2018-05-28T21:53:00Z',0.00000848,0.00000847,0.00000850,0.00000846,12708.9320888]]
const keys = ['T', 'O', 'C', 'H', 'L', 'V'];
const arrs = input.reduce((accum, item) => {
keys.forEach((key, i) => accum[key].push(item[i]));
return accum;
}, keys.reduce((a, key) => { a[key] = []; return a }, {}));
console.log(arrs);
Upvotes: 3