reduce滥用记录

博客探讨了在处理数据时reduce方法的误用情况,包括原始数据、错误处理结果、期望结果以及逐步修正的过程,最终给出了正确的实现版本。
摘要由CSDN通过智能技术生成

原始数据:

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
                    };
                }, {}))
            ];
        }, []));
    }, []));
}, []);

有空再分析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值