Closed. This question needs to be more focused。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                
                    3年前关闭。
            
        

    

我有这些数据,我无法控制其格式或进行任何更改

//input json data
[
    {
        "Breaks":[
                {"points":12,"points_total":12,"average":8.0,"faults":[]},
                {"points":17,"points_total":29,"average":11.6,"faults":[]},
                {"points":6,"points_total":35,"average":11.6667,"faults":[]},
                {"points":8,"points_total":43,"average":10.75,"faults":[]},
                {"points":14,"points_total":57,"average":11.4,"faults":[]},
            ],
        "team_name":"King Sports"
    },

    {
        "Breaks":[
            {"points":18,"points_total":18,"average":15.4286,"faults":[]},
            {"points":2,"points_total":20,"average":10.0,"faults":[]},
            {"points":7,"points_total":27,"average":9.0,"faults":[]},
            {"points":9,"points_total":36,"average":9.0,"faults":[]},
            {"points":4,"points_total":40,"average":8.0,"faults":[]},
            {"points":4,"points_total":44,"average":7.33333,"faults":[]},
            {"points":4,"points_total":48,"average":6.85714,"faults":[]},
            {"points":8,"points_total":56,"average":7.0,"faults":[]},
            {"points":1,"points_total":57,"average":6.33333,"faults":[]},
            {"points":6,"points_total":63,"average":6.3,"faults":[]},
            {"points":3,"points_total":66,"average":5.82353,"faults":[]},
            {"points":6,"points_total":72,"average":6.0,"faults":[]},
            {"points":7,"points_total":79,"average":6.07692,"faults":[]},
            {"points":3,"points_total":82,"average":5.85714,"faults":[]},
            {"points":0,"points_total":82,"average":5.65517,"faults":[]}
        ],
    "team_name":"Lion Sports"
    }
]


因此,我需要对其进行重建/重构,以获得以下结果。
如果在“ Breaks”中找不到任何值,则将有20个“ Breaks”,直到达到20,则应具有“ null”值。

 //the result what i wanted = output expected

 [
     ['Breaks', 'King Sports', 'Lion Sports'],
     ['1',  12, 18],
     ['2',  29, 20],
     ['3',  35, 27],
     ['4',  43, 36],
     ['5',  57, 40],
     ['6',  null, 44],
     ['7',  null, 48],
     ['8',  null, 56],
     ['9',  null, 57],
     ['10',  null, 63],
     ['11',  null, 66],
     ['12', null, 72],
     ['13',  null, 79],
     ['14',  null, 82],
     ['15',  null, null],
     ['16',  null, null],
     ['17',  null, null],
     ['18',  null, null],
     ['19',  null, null],
     ['20',  null, null]

 ]

最佳答案

您可以先生成结果数组,然后将值放入。



var data = [{ Breaks: [{ points: 12, points_total: 12, average: 8.0, faults: [] }, { points: 17, points_total: 29, average: 11.6, faults: [] }, { points: 6, points_total: 35, average: 11.6667, faults: [] }, { points: 8, points_total: 43, average: 10.75, faults: [] }, { points: 14, points_total: 57, average: 11.4, faults: [] }], team_name: "King Sports" }, { Breaks: [{ points: 18, points_total: 18, average: 15.4286, faults: [] }, { points: 2, points_total: 20, average: 10.0, faults: [] }, { points: 7, points_total: 27, average: 9.0, faults: [] }, { points: 9, points_total: 36, average: 9.0, faults: [] }, { points: 4, points_total: 40, average: 8.0, faults: [] }, { points: 4, points_total: 44, average: 7.33333, faults: [] }, { points: 4, points_total: 48, average: 6.85714, faults: [] }, { points: 8, points_total: 56, average: 7.0, faults: [] }, { points: 1, points_total: 57, average: 6.33333, faults: [] }, { points: 6, points_total: 63, average: 6.3, faults: [] }, { points: 3, points_total: 66, average: 5.82353, faults: [] }, { points: 6, points_total: 72, average: 6.0, faults: [] }, { points: 7, points_total: 79, average: 6.07692, faults: [] }, { points: 3, points_total: 82, average: 5.85714, faults: [] }, { points: 0, points_total: 82, average: 5.65517, faults: [] }], team_name: "Lion Sports" }],
    result = data.reduce(function (r, a, i) {
        r[0][i + 1] = a.team_name;
        a.Breaks.forEach(function (b, j) {
            r[j + 1][i + 1] = b.points_total;
        });
        return r;
    }, function (length) {
        var a = Array.apply(null, { length: length + 1 }).map(function (_, i) { return [(i || 'Breaks').toString(), null, null] });
        return a;
    }(20));

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

10-06 08:30