V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
rateltalk
V2EX  ›  问与答

如何使用 JS 对比数组对象并用 Table 绘制结果?

  •  
  •   rateltalk · Dec 11, 2018 · 1118 views
    This topic created in 2697 days ago, the information mentioned may be changed or developed.

    原始数据:

    var data = [
        {
        	title: "张三",
            sex: "男",
            age: "22",
            address: "吉祥村"
        },
        {
        	title: "李四",
            sex: "男",
            age: "24",
            address: "鱼化寨"
        }
    ];
    

    如上,如何对比数组中的两个或者多个对象,并用 Table 绘制,期望如下:

    title   | 张三   | 李四
    ------- | -----  | ------
    sex     | 男     | 男
    age     | 22     | 24
    address | 吉祥村 | 鱼化寨
    
    Supplement 1  ·  Dec 12, 2018

    附上代码:

    var arr = [];
    
    for(var i in data[0]){
        arr.push([i])
    }
    
    for(var i in data){
        for(var j in data[i]){
            for(var k in arr){
                if(arr[k][0]===j){
                    arr[k].push(data[i][j]);
                }
            }
        }
    }
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2757 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 88ms · UTC 14:23 · PVG 22:23 · LAX 07:23 · JFK 10:23
    ♥ Do have faith in what you're doing.