原始数据:
let data = [
{
"index": 0,
"data": [
{"key": "index","type": "number","value": 0,"editable": false,
"options": {}
},
{"key": "canid","type": "text","value": "0000","editable": true,
"options": {"maxLength": 4}
},
{"key": "content","type": "parent",
"children": [
{
"index": 0,
"data": [
{"key": "id","type": "number","value": 0,"editable": false,
"options": {}
},
{"key": "in_offset","type": "text","value": "0", "editable": true,
"options": {}
},
{"key": "in_len","type": "number","value": 0,"editable": true,
"options": {}
}
]
},
{
"index": 1,
"data": [
{"key": "id","type": "number","value": 0,"editable": false,
"options": {}
},
{"key": "in_offset","type": "text","value": "0", "editable": true,
"options": {}
},
{"key": "in_len","type": "number","value": 0,"editable": true,
"options": {}
}
]
},
{
"index": 2,
"data": [
{"key": "id","type": "number","value": 0,"editable": false,
"options": {}
},
{"key": "in_offset","type": "text","value": "0", "editable": true,
"options": {}
},
{"key": "in_len","type": "number","value": 0,"editable": true,
"options": {}
}
]
}
]
}
]
},
{
"index": 1,
"data": [
{"key": "index","type": "number","value": 0,"editable": false,
"options": {}
},
{"key": "canid","type": "text","value": "0000","editable": true,
"options": {"maxLength": 4}
},
{"key": "content","type": "parent",
"children": [
{
"index": 0,
"data": [
{"key": "id","type": "number","value": 0,"editable": false,
"options": {}
},
{"key": "in_offset","type": "text","value": "0", "editable": true,
"options": {}
},
{"key": "in_len","type": "number","value": 0,"editable": true,
"options": {}
}
]
},
{
"index": 1,
"data": [
{"key": "id","type": "number","value": 0,"editable": false,
"options": {}
},
{"key": "in_offset","type": "text","value": "0", "editable": true,
"options": {}
},
{"key": "in_len","type": "number","value": 0,"editable": true,
"options": {}
}
]
},
{
"index": 2,
"data": [
{"key": "id","type": "number","value": 0,"editable": false,
"options": {}
},
{"key": "in_offset","type": "text","value": "0", "editable": true,
"options": {}
},
{"key": "in_len","type": "number","value": 0,"editable": true,
"options": {}
}
]
}
]
}
]
}
];
处理错误结果:
期望及处理成功结果:
一开始处理错误版本:
let data1 = data.reduce((previousValue, currentValue, currentIndex) => {
return previousValue.concat(currentValue.data.reduce((previousValue, currentValue, currentIndex) => {
if (currentValue.type !== 'parent') {
return previousValue.concat({
[currentValue.key]: currentValue.value
});
}
return previousValue.concat(currentValue.children.reduce((previousValue, currentValue, currentIndex) => {
return previousValue.concat(currentValue.data.reduce((previousValue, currentValue, currentIndex) => {
return previousValue.concat({
[currentValue.key]: currentValue.value
})
}, []));
}, []));
}, []));
}, []);
最后的正确版本:
let data2 = data.reduce((previousValue, currentValue, currentIndex) => {
let _currentValue = currentValue;
return previousValue.concat(currentValue.data.filter(value => value.type === 'parent' && value.hasOwnProperty('children')).reduce((previousValue, currentValue, currentIndex) => {
return previousValue.concat(currentValue.children.reduce((previousValue, currentValue, currentIndex) => {
return [
...previousValue,
currentValue.data.reduce((previousValue, currentValue, currentIndex) => {
return {
...previousValue,
[currentValue.key]: currentValue.value
};
}, _currentValue.data.filter(value => value.type !== 'parent').reduce((previousValue, currentValue, currentIndex) => {
return {
...previousValue,
[currentValue.key]: currentValue.value
};
}, {}))
];
}, []));
}, []));
}, []);
有空再分析