我有这个数组:
[ [ '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 ]
]
我需要将其转换为:
{
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 ]
}
它是预定义的地方,第一个值始终是' T'第二个值始终是' O'等等。
尝试像这样的lodash
_.zipObject(['T', 'O', 'H', 'L', 'C'], s.values)
但结果不是预期的。一些建议?
答案 0 :(得分:3)
不需要库,您可以使用普通的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);
答案 1 :(得分:2)
你可以通过使用zip和zipobject来实现:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
&#13;
{{1}}&#13;
答案 2 :(得分:0)
您可以通过以下方式实现此目的:
const trans = data[0].map((col, i) => data.map(row => row[i]));
_.zipObject(['T', 'O', 'H', 'L', 'C'], trans)
数据是您的初始数组。