【问题标题】:Applying reduce on an object and getting a set of objects对一个对象应用 reduce 并获取一组对象
【发布时间】:2021-03-27 13:44:31
【问题描述】:

我有一个对象,我需要使用 reduce 对其进行转换,但无法做到... 我有以下对象:

const object = {
    set: {
        subset: [
        {
            subset_X: 1,
            algorithms: [
                {
                    type: "algorithm_A",
                    params: {
                        algorithm_A_param_1: 0.1,
                        algorithm_A_param_2: 0.2
                    }
                },
                {
                    type: "algorithm_B",
                    params: {
                        algorithm_B_param_1: 0.3,
                        algorithm_B_param_2: 0.4
                    }
                }
            ],
            subset_collection: [
                {
                    collection_X: 1,
                    algorithms: [
                        {
                            type: "algorithm_a",
                            params: {
                                algorithm_a_param_1: 0.5,
                                algorithm_a_param_2: 0.6
                            }
                        },
                        {
                            type: "algorithm_b",
                            params: {
                                algorithm_b_param_1: 0.7,
                                algorithm_b_param_2: 0.8
                            }
                        }
                    ],
                    collection_subcollection: [
                        {
                            start: 1,
                            algorithms: [
                                {
                                    type: "algorithm_1",
                                    params: {
                                        algorithm_1_param_1: 10,
                                        algorithm_1_param_2: 20
                                    }
                                },
                                {
                                    type: "algorithm_11",
                                    params: {
                                        algorithm_11_param_1: 30,
                                        algorithm_11_param_2: 40
                                    }
                                }
                            ]
                        },
                        {
                            start: 2,
                            algorithms: [
                                {
                                    type: "algorithm_1",
                                    params: {
                                        algorithm_1_param_1: 50,
                                        algorithm_1_param_2: 60
                                    }
                                },
                                {
                                    type: "algorithm_11",
                                    params: {
                                        algorithm_11_param_1: 70,
                                        algorithm_11_param_2: 80
                                    }
                                }
                            ]
                        }
                    ]
                },
                {
                    collection_X: 2,
                    algorithms: [
                        {
                            type: "algorithm_a",
                            params: {
                                algorithm_a_param_1: 0.9,
                                algorithm_a_param_2: 1
                            }
                        },
                        {
                            type: "algorithm_b",
                            params: {
                                algorithm_b_param_1: 1.1,
                                algorithm_b_param_2: 1.2
                            }
                        }
                    ],
                    collection_subcollection: [
                        {
                            start: 1,
                            algorithms: [
                                {
                                    type: "algorithm_1",
                                    params: {
                                        algorithm_1_param_1: 90,
                                        algorithm_1_param_2: 100
                                    }
                                },
                                {
                                    type: "algorithm_11",
                                    params: {
                                        algorithm_11_param_1: 110,
                                        algorithm_11_param_2: 120
                                    }
                                }
                            ]
                        },
                        {
                            start: 2,
                            algorithms: [
                                {
                                    type: "algorithm_1",
                                    params: {
                                        algorithm_1_param_1: 130,
                                        algorithm_1_param_2: 140
                                    }
                                },
                                {
                                    type: "algorithm_11",
                                    params: {
                                        algorithm_11_param_1: 150,
                                        algorithm_11_param_2: 160
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            subset_X: 2,            
            alg: [
                {
                    type: "algorithm_C",
                    params: {
                        algorithm_C_param_1: 1.3,
                        algorithm_C_param_2: 1.4
                    }
                },
                {
                    type: "algorithm_D",
                    params: {
                        algorithm_D_param_1: 1.5,
                        algorithm_D_param_2: 1.6
                    }
                }
            ],
            subset_collection: [
                {
                    collection_X: 1,
                    alg: [
                        {
                            type: "algorithm_a",
                            params: {
                                algorithm_a_param_1: 1.7,
                                algorithm_a_param_2: 1.8
                            }
                        },
                        {
                            type: "algorithm_b",
                            params: {
                                algorithm_b_param_1: 1.9,
                                algorithm_b_param_2: 2
                            }
                        }
                    ],
                    collection_subcollection: [
                        {
                            start: 1,
                            algorithms: [
                                {
                                    type: "algorithm_1",
                                    params: {
                                        algorithm_1_param_1: 170,
                                        algorithm_1_param_2: 180
                                    }
                                },
                                {
                                    type: "algorithm_11",
                                    params: {
                                        algorithm_11_param_1: 190,
                                        algorithm_11_param_2: 200
                                    }
                                }
                            ]
                        },
                        {
                            start: 2,
                            algorithms: [
                                {
                                    type: "algorithm_1",
                                    params: {
                                        algorithm_1_param_1: 210,
                                        algorithm_1_param_2: 220
                                    }
                                },
                                {
                                    type: "algorithm_11",
                                    params: {
                                        algorithm_11_param_1: 230,
                                        algorithm_11_param_2: 240
                                    }
                                }
                            ]
                        }
                    ]
                },
                {
                    collection_X: 2,
                    alg: [
                        {
                            type: "algorithm_a",
                            params: {
                                algorithm_a_param_1: 2.1,
                                algorithm_a_param_2: 2.2
                            }
                        },
                        {
                            type: "algorithm_b",
                            params: {
                                algorithm_b_param_1: 2.3,
                                algorithm_b_param_2: 2.4
                            }
                        }
                    ],
                    collection_subcollection: [
                        {
                            start: 1,
                            algorithms: [
                                {
                                    type: "algorithm_1",
                                    params: {
                                        algorithm_1_param_1: 250,
                                        algorithm_1_param_2: 260
                                    }
                                },
                                {
                                    type: "algorithm_11",
                                    params: {
                                        algorithm_11_param_1: 270,
                                        algorithm_11_param_2: 280
                                    }
                                }
                            ]
                        },
                        {
                            start: 2,
                            algorithms: [
                                {
                                    type: "algorithm_1",
                                    params: {
                                        algorithm_1_param_1: 290,
                                        algorithm_1_param_2: 300
                                    }
                                },
                                {
                                    type: "algorithm_11",
                                    params: {
                                        algorithm_11_param_1: 310,
                                        algorithm_11_param_2: 320
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        }
        ]
    }
}

我需要获得的是一个对象,其属性/字段是来自初始对象的多个字段的串联:“subset_X:collection_X:start:algorithm_{X}”

我需要获取以下对象:

{
    "1:::algorithm_A": {
        algorithm_A_param_1: 0.1,
        algorithm_A_param_2: 0.2
    },
    "1:::algorithm_A": {
        algorithm_A_param_1: 0.3,
        algorithm_A_param_2: 0.4
    },
    "1:1:1:algorithm_1": {
        algorithm_1_param_1: 10,
        algorithm_1_param_2: 20
    },
    "1:1:1:algorithm_11": {
        algorithm_11_param_1: 30,
        algorithm_11_param_2: 40
    },
    "1:1::algorithm_a": {
        algorithm_a_param_1: 0.5,
        algorithm_a_param_2: 0.6
    },
    "1:1::algorithm_b": {
        algorithm_b_param_1: 0.7,
        algorithm_b_param_2: 0.8
    },
    "1:1:2:algorithm_1": {
        algorithm_1_param_1: 50,
        algorithm_1_param_2: 60
    },
    "1:1:2:algorithm_11": {
        algorithm_11_param_1: 70,
        algorithm_11_param_2: 80
    },
    "1:2::algorithm_a": {
        algorithm_a_param_1: 0.9,
        algorithm_a_param_2: 1
    },
    "1:2::algorithm_b": {
        algorithm_b_param_1: 1.1,
        algorithm_b_param_2: 1.2
    },
    "1:2:1:algorithm_1": {
        algorithm_1_param_1: 90,
        algorithm_1_param_2: 100
    },
    "1:2:1:algorithm_11": {
        algorithm_11_param_1: 110,
        algorithm_11_param_2: 120
    },
    "1:2:2:algorithm_1": {
        algorithm_1_param_1: 130,
        algorithm_1_param_2: 140
    },
    "1:2:2:algorithm_11": {
        algorithm_11_param_1: 150,
        algorithm_11_param_2: 160
    },
    "2:::algorithm_C": {
        algorithm_C_param_1: 1.3,
        algorithm_C_param_2: 1.4
    },
    "2:::algorithm_D": {
        algorithm_D_param_1: 1.5,
        algorithm_D_param_2: 1.6
    },
    "2:1:1:algorithm_1": {
        algorithm_1_param_1: 170,
        algorithm_1_param_2: 180
    },
    "2:1:1:algorithm_11": {
        algorithm_11_param_1: 190,
        algorithm_11_param_2: 200
    },
    "2:1::algorithm_a": {
        algorithm_a_param_1: 1.7,
        algorithm_a_param_2: 1.8
    },
    "2:1::algorithm_b": {
        algorithm_b_param_1: 1.9,
        algorithm_b_param_2: 2
    },
    "2:1:2:algorithm_1": {
        algorithm_1_param_1: 210,
        algorithm_1_param_2: 220
    },
    "2:1:2:algorithm_11": {
        algorithm_11_param_1: 230,
        algorithm_11_param_2: 240
    },
    "2:2:1:algorithm_1": {
        algorithm_1_param_1: 250,
        algorithm_1_param_2: 260
    },
    "2:2:1:algorithm_11": {
        algorithm_11_param_1: 270,
        algorithm_11_param_2: 280
    },
    "2:2::algorithm_a": {
        algorithm_a_param_1: 2.1,
        algorithm_a_param_2: 2.2
    },
    "2:2::algorithm_b": {
        algorithm_b_param_1: 2.3,
        algorithm_b_param_2: 2.4
    },
    "2:2:2:algorithm_1": {
        algorithm_1_param_1: 290,
        algorithm_1_param_2: 300
    },
    "2:2:2:algorithm_11": {
        algorithm_11_param_1: 310,
        algorithm_11_param_2: 320
    }
}

请帮忙:(谢谢!

我尝试的是使用键获取数组:

var ruleIds = [...new Set([...object.set.subset.flatMap(subset => subset.algorithms.flatMap(alg => `${subset.subset_X}:::${alg.type}`).concat(subset.subset_collection.flatMap(col => col.algorithms.flatMap(alg => `${subset.subset_X}:${col.collection_X}::${alg.type}`).concat(col.collection_subcollection.flatMap(subCol => subCol.algorithms.flatMap(alg => `${subset.subset_X}:${col.collection_X}:${subCol.startBit}:${alg.type}`))))))])]

想要获得:

["1:::algorithm_A", "1:::algorithm_A", "1:1:1:algorithm_1" ..... ]

但是之后就不知道怎么加参数了……

【问题讨论】:

  • 你的代码哪里出了问题?你控制输入对象吗?输入对象和预期输出表现出对如何使用对象和数组的误解。
  • 我投票结束这个问题,因为它不包括解决问题的尝试
  • 这不是我的代码的问题,但经过几次我试图转换它以获得第二个对象......我没有成功:(
  • 请包含尝试转换输入的代码。尝试在哪里失败?

标签: javascript arrays reactjs ecmascript-6 reduce


【解决方案1】:

您可以使用递归在数据结构中执行深度优先遍历。

我对数据结构做了一些假设,包括:

  • 这棵树的“叶子”总是有一个“params”属性。
  • 树中的其他节点最多有 2 个属性,其中一个是数组。另一个可选属性定义路径的一部分
  • 由于上述情况不适用于顶部节点(因为object 没有数组属性),因此需要使用子属性object.set 调用此实现。

我使用了一个生成器,所以被调用者可以使用Array.from从它构造一个数组:

function * flatten(object, path = "") {
    if ("params" in object) return yield { [path + object.type]: object.params };
    let entries = Object.entries(object);
    if (entries.length > 2 || !entries.length) throw "unexpected object structure";
    if (entries.length < 2) entries.unshift(["", ""]); // e.g. "set" has no path-element property
    else if (Array.isArray(entries[0][1])) entries.reverse();
    let [id, arr] = entries.map(([, v]) => v);
    if (!Array.isArray(arr)) throw "unexpected object structure";
    for (let child of arr) {
        yield * flatten(child, id ? path + id + ":" : path);
    }
}

// The example input given in the question:
const object = {set: {subset: [{subset_X: 1,subset_collection: [{collection_X: 1,collection_subcollection: [{start: 1,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 10,algorithm_1_param_2: 20}},{type: "algorithm_11",params: {algorithm_11_param_1: 30,algorithm_11_param_2: 40}}]},{start: 2,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 50,algorithm_1_param_2: 60}},{type: "algorithm_11",params: {algorithm_11_param_1: 70,algorithm_11_param_2: 80}}]}]},{collection_X: 2,collection_subcollection: [{start: 1,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 90,algorithm_1_param_2: 100}},{type: "algorithm_11",params: {algorithm_11_param_1: 110,algorithm_11_param_2: 120}}]},{start: 2,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 130,algorithm_1_param_2: 140}},{type: "algorithm_11",params: {algorithm_11_param_1: 150,algorithm_11_param_2: 160}}]}]}]},{subset_X: 2,subset_collection: [{collection_X: 1,collection_subcollection: [{start: 1,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 170,algorithm_1_param_2: 180}},{type: "algorithm_11",params: {algorithm_11_param_1: 190,algorithm_11_param_2: 200}}]},{start: 2,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 210,algorithm_1_param_2: 220}},{type: "algorithm_11",params: {algorithm_11_param_1: 230,algorithm_11_param_2: 240}}]}]},{collection_X: 2,collection_subcollection: [{start: 1,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 250,algorithm_1_param_2: 260}},{type: "algorithm_11",params: {algorithm_11_param_1: 270,algorithm_11_param_2: 280}}]},{start: 2,algorithms: [{type: "algorithm_1",params: {algorithm_1_param_1: 290,algorithm_1_param_2: 300}},{type: "algorithm_11",params: {algorithm_11_param_1: 310,algorithm_11_param_2: 320}}]}]}]}]}};

let result = Array.from(flatten(object.set));
console.log(result);

【讨论】:

  • 感谢您的回答,但我正在寻找更简单的东西,使用reduce,flatMap ... PS:我修改了对象..和结果
  • 祝你好运,“简单得多”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 2022-11-17
  • 2019-07-04
  • 2021-11-06
  • 2022-01-24
相关资源
最近更新 更多