Solving the problem of an js array

var cloumns = [
            ["", "", "", 287589],
            ["", "", "", 731786],
            ["", "", "", -4554],
            ["", "", "", -13556],
            ["", "", "", 6883],
            ["", "", "", 266137],
            ["", "", "", 113187],
            ["", "", "", 329072],
            ["", "", "", 614692],
            ["", "", "", 229967],
            ["", "", "", 66641],
            ["", "", "", 294351],
            ["", "", "", 52]
        ];

as in the previous array, how to convert it to this form:

var translateData = {
            nodes: [
                {
                    name: "",
                    nodes: [
                        {
                            name: "",
                            nodes: [
                                {
                                    name: "",
                                    value: 287589
                                },
                                {
                                    name: "",
                                    value: 731786
                                }
                            ]
                        },
                        {
                            name: "",
                            nodes: [
                                {
                                    name: "",
                                    value: -4554
                                },
                                {
                                    name: "",
                                    value: -13556
                                }
                            ]
                        }
                    ]
                }, {
                    name: "",
                    nodes: [
                        {
                            name: "",
                            nodes: [
                                {
                                    name: "",
                                    value: 287589
                                }
                            ]
                        }
                    ]
                }
            ]
        }

first extract the first identical array to form an array. And then traverse them separately. The data structure is up to you


        //3
        const cloumns = [
            ["", "", "", 287589],
            ["", "", "", 731786],
            ["", "", "", -4554],
            ["", "", "", -13556],
            ["", "", "", 6883],
            ["", "", "", 266137],
            ["", "", "", 113187],
            ["", "", "", 329072],
            ["", "", "", 614692],
            ["", "", "", 229967],
            ["", "", "", 66641],
            ["", "", "", 294351],
            ["", "", "", 52]
        ]

        //
        const nodes = [];
        let first, second;

        //cloumns
        cloumns.forEach(row => {
            //row[0],,
            const firstFlag = nodes.some(firstNode => {
                if (firstNode.name == row[0]) first = firstNode;
                return firstNode.name == row[0]
            })

            if (!firstFlag) {
                first = {
                    name: row[0],
                    nodes: []
                }
                nodes.push(first)
            }

            //row[1],,
            const secondFlag = first.nodes.some(secondNode => {
                if (secondNode.name == row[1]) second = secondNode;
                return secondNode.name == row[1]
            })

            if (!secondFlag) {
                second = {
                    name: row[1],
                    nodes: []
                }
                first.nodes.push(second)
            }

            //,push
            second.nodes.push({
                name: row[2],
                value: row[3]
            })
        })

function trans (arr) {
    let result = {nodes: []}
    let depth = arr[0].length - 1
    if (!arr.length || depth < 1) return obj

    let cache = {
        '': result.nodes
    }

    ;[...new Array(depth).keys()].map(i => +i).forEach(i => {
        arr.forEach(cur => {
            let key = cur[i]
            let allKey = cur.reduce((sum, cur, j) => sum += j < i ? cur : '', '')
            if (i === depth - 1) {
                return cache[allKey].push({name: key, value: cur[i + 1]})
            }
            if (!cache[allKey + key]) {
                let obj = {name: key, nodes: []}
                cache[allKey].push(obj)
                cache[allKey + key] = obj.nodes
            }
        })
    })
    return result
}
trans(cloumns)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36bc8-2b88d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36bc8-2b88d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?