-
-
Save mutoo/5cb1b48359049c29fcac7563d8cf3200 to your computer and use it in GitHub Desktop.
Answer to https://www.v2ex.com/t/549747
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 插入时间 | |
const timeData = ['1:00', '2:00', '3:00', '4:00', '5:00', '6:00']; | |
// 源数据 | |
const sourceData = { | |
parent1: { | |
child1: [1, 2, 3, 4, 5, 6], | |
child2: [11, 22, 33, 44, 55, 66], | |
child3: [111, 222, 333, 444, 555, 666], | |
}, | |
parent2: { | |
child1: [10, 20, 30, 40, 50, 60], | |
child2: [110, 220, 330, 440, 550, 660], | |
child3: [1110, 2220, 3330, 4440, 5550, 6660], | |
}, | |
parent3: { | |
child1: [101, 202, 303, 404, 505, 606], | |
child2: [1101, 2202, 3303, 4404, 5505, 6606], | |
child3: [11101, 22202, 33303, 44404, 55505, 66606], | |
}, | |
}; | |
// Object 的 keys 的顺序是不确定的,所以 Object.keys() 不能保证结果 array 的顺序。 | |
// 这里直接用 hard-coded 演示,如果可以排序,可以考虑用 Object.keys(sourceData).sort() | |
const outputData = []; | |
['parent1', 'parent2', 'parent3'].reduce((output, parentKey) => { | |
// 子条目用的是 Object,所以与 key 顺序无关,可以直接取 | |
const childKeys = Object.keys(sourceData[parentKey]); | |
timeData.forEach((timeVal, idx) => { | |
output.push(childKeys.reduce((itemRet, childKey) => { | |
itemRet[childKey] = {value: sourceData[parentKey][childKey][idx]}; | |
return itemRet; | |
}, {time: {value: timeVal}})); | |
}); | |
return output; | |
}, outputData); | |
console.log(outputData); | |
/** | |
outputData = [ | |
{ | |
time: {value: '1:00'}, | |
child1: {value: 1}, | |
child2: {value: 11}, | |
child3: {value: 111}, | |
}, | |
{ | |
time: {value: '2:00'}, | |
child1: {value: 2}, | |
child2: {value: 22}, | |
child3: {value: 222}, | |
}, | |
{ | |
time: {value: '3:00'}, | |
child1: {value: 3}, | |
child2: {value: 33}, | |
child3: {value: 333}, | |
}, | |
{ | |
time: {value: '4:00'}, | |
child1: {value: 4}, | |
child2: {value: 44}, | |
child3: {value: 444}, | |
}, | |
{ | |
time: {value: '5:00'}, | |
child1: {value: 5}, | |
child2: {value: 55}, | |
child3: {value: 555}, | |
}, | |
{ | |
time: {value: '6:00'}, | |
child1: {value: 6}, | |
child2: {value: 66}, | |
child3: {value: 666}, | |
}, | |
{ | |
time: {value: '1:00'}, | |
child1: {value: 10}, | |
child2: {value: 110}, | |
child3: {value: 1110}, | |
}, | |
{ | |
time: {value: '2:00'}, | |
child1: {value: 20}, | |
child2: {value: 220}, | |
child3: {value: 2220}, | |
}, | |
{ | |
time: {value: '3:00'}, | |
child1: {value: 30}, | |
child2: {value: 330}, | |
child3: {value: 3330}, | |
}, | |
{ | |
time: {value: '4:00'}, | |
child1: {value: 40}, | |
child2: {value: 440}, | |
child3: {value: 4440}, | |
}, | |
{ | |
time: {value: '5:00'}, | |
child1: {value: 50}, | |
child2: {value: 550}, | |
child3: {value: 5550}, | |
}, | |
{ | |
time: {value: '6:00'}, | |
child1: {value: 60}, | |
child2: {value: 660}, | |
child3: {value: 6660}, | |
}, | |
{ | |
time: {value: '1:00'}, | |
child1: {value: 101}, | |
child2: {value: 1101}, | |
child3: {value: 11101}, | |
}, | |
{ | |
time: {value: '2:00'}, | |
child1: {value: 202}, | |
child2: {value: 2202}, | |
child3: {value: 22202}, | |
}, | |
{ | |
time: {value: '3:00'}, | |
child1: {value: 303}, | |
child2: {value: 3303}, | |
child3: {value: 33303}, | |
}, | |
{ | |
time: {value: '4:00'}, | |
child1: {value: 404}, | |
child2: {value: 4404}, | |
child3: {value: 44404}, | |
}, | |
{ | |
time: {value: '5:00'}, | |
child1: {value: 505}, | |
child2: {value: 5505}, | |
child3: {value: 55505}, | |
}, | |
{ | |
time: {value: '6:00'}, | |
child1: {value: 606}, | |
child2: {value: 6606}, | |
child3: {value: 66606}, | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment