Как манипулировать массивом объектов javascript с помощью alasql и создавать новое представление? - PullRequest
0 голосов
/ 09 января 2019

Если мы используем 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"
        }
    }
]
...