如何使用alasql操作对象的javascript数组并创建新视图?

时间:2019-01-09 15:35:17

标签: alasql

如果我们使用的是alasql,并且我们有一个包含以下结构的数据数组:

const listOfX = [
    {
        id: 1,
        title: "row 1 of table X",
        listOfY: [
            {
                id: 1,
                title: "row 1 of table Y",
                listOfZ: [
                    {
                        id: 1,
                        title: "row 1 of table Z"
                    },
                    {
                        id: 2,
                        title: "row 2 of table Z"
                    },
                    {
                        id: 3,
                        title: "row 3 of table Z"
                    },
                ]
            },
            {
                id: 2,
                title: "row 2 of table Y",
                listOfZ: [
                    {
                        id: 4,
                        title: "row 4 of table Z"
                    },
                    {
                        id: 5,
                        title: "row 5 of table Z"
                    },
                    {
                        id: 6,
                        title: "row 6 of table Z"
                    },
                ]
            }
        ]
    },
    {
        id: 2,
        title: "row 2 of table X",
        listOfY: [
            {
                id: 3,
                title: "row 3 of table Y",
                listOfZ: [
                    {
                        id: 7,
                        title: "row 7 of table Z"
                    },
                    {
                        id: 8,
                        title: "row 8 of table Z"
                    },
                    {
                    id: 9,
                    title: "row 9 of table Z"
                    },
                ]
            },
            {
                id: 4,
                title: "row 4 of table Y",
                listOfZ: [
                    {
                        id: 10,
                        title: "row 10 of table Z"
                    },
                    {
                        id: 11,
                        title: "row 11 of table Z"
                    },
                    {
                        id: 12,
                        title: "row 12 of table Z"
                    },
                ]
            }
        ]
    }
];

也就是说,它包含一个结构,其中表X包含表Y的对象列表,表Y包含表Z的对象列表。 我们如何才能反转它并创建这样的视图? :

const expected = [
    {
        id: 1,
        title: "row 1 of table Z",
        y: {
            id: 1,
            title: "row 1 of table Y"
        },
        x: {
            id: 1,
            title: "row 1 of table X"
        }
    },
    {
        id: 2,
        title: "row 2 of table Z",
        y: {
            id: 1,
            title: "row 1 of table Y"
        },
        x: {
            id: 1,
            title: "row 1 of table X"
        }
    },
    ...
    {
        id: 12,
        title: "row 12 of table Z",
        y: {
            id: 4,
            title: "row 4 of table Y"
        },
        x: {
            id: 2,
            title: "row 1 of table X"
        }
    }
]

0 个答案:

没有答案