点击查看html编辑器说明文档

data to tableedit icon

|
|
Fork(复制)
|
|
作者:
lynn-ssk
提交反馈
嵌入
设置
下载
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ...
展开
</head>
<body>
            
            
        
</body>
CSS
格式化
            
            
        
JS
格式化
            
            function toTable(data, columns) {
    let idxs = [];
    let keys = columns || [];
    let vals = {};
    for (let i in data) {
        idxs.push(i);
        for (k of keys) {
            if (k in data[i]) {
                vals[i + '_' + k] = data[i][k];
            }
        }
        if (!columns || columns.length === 0) {
            let leftNumber = 20 - keys.length;
            if (leftNumber > 0) {
                for (k in data[i]) {
                    if (leftNumber > 0) {
                        if (!keys.includes(k)) {
                            keys.push(k);
                            vals[i + '_' + k] = data[i][k];
                            leftNumber--;
                        }
                    } else {
                        break;
                    }
                }
            }
        }
    }
    return {
        idxs,
        keys,
        vals
    };
}

console.log(toTable([{
        id: 1,
        name: 2,
        key2: 33,
        key4: 55,
        keyy5: 66,
        key6: 666,
        key7: 77
    },
    {
        id: 3,
        name: 4
    },
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
],["2","3", 'id']));
        
预览
控制台
清空