将json响应转换为表

时间:2019-05-09 06:26:30

标签: javascript json typescript

我正在尝试将json响应转换为接受我的表(来自devextreme的组件)的数组。我正在尝试在角度6上执行此操作。Json看起来像这样:

"data": [
            {
                "type": "A",
                "date": "2018-05",
                "value": "153"
            },
            {
                "type": "B",
                "date": "2018-05",
                "value": "888"
            },
            {
                "type": "C",
                "date": "2018-05",
                "value": "999"
            },
            {
                "type": "D",
                "date": "2018-05",
                "value": "555"
            },
            {
                "type": "A",
                "date": "2018-06",
                "value": "148"
            },
            {
                "type": "B",
                "date": "2018-06",
                "value": "222"
            },
            {
                "type": "C",
                "date": "2018-06",
                "value": "555"
            },
            {
                "type": "D",
                "date": "2018-06",
                "value": "666"
            },
            {
                "type": "A",
                "date": "2018-07",
                "value": "156"
            },
            {
                "type": "B",
                "date": "2018-07",
                "value": "111"
            },
            {
                "type": "C",
                "date": "2018-07",
                "value": "333"
            },
            {
                "type": "D",
                "date": "2018-07",
                "value": "999"
            }
],
        "number": "111-111"
   }
]

我需要将其转换为以下格式:

[{
    month: '2018-05',
    A: 153,
    B: 888,
    C: 999,
    D: 555
  },
  {
    month: '2018-06',
    A: 148,
    B: 222,
    C: 555,
    D: 666
  },
  {
    month: '2018-07',
    A: 156,
    B: 111,
    C: 333,
    D: 999
  }]

类型数可以更改(例如,只能有A和B)。有人可以帮我吗?我正在使用此组件在网站https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/SimpleArray/Angular/Light/

上显示数据

3 个答案:

答案 0 :(得分:2)

这就是我要解决您的问题的方式:

1)从数据源获取类型和日期的列表。我们使用JavaScript的Set存储值,以便这些值是唯一的(没有重复项)。

来自date的值将被视为结果数组中每个对象的唯一键,而type将是结果的其他属性(“ A”,“ B”, “ C”和“ D”)

2)一旦有了这2个数组,我们将遍历原始列表以生成对象。通过从原始{{1}中过滤AB来填充属性CDdatetype的值}。

list

答案 1 :(得分:0)

您可以尝试这样

TS

let finalArray = [];
let valueA: string = null;
let valueB: string = null;
let valueC: string = null;
// Here data is a variable which contain your JSON data
for(let i = 0; i < data.length; i++) {
    if (data.type === 'A') { 
      valueA = data[i].value;
    }
    if (data.type === 'B') { 
      valueB = data[i].value;
    } 
    if (data.type === 'C') { 
      valueC = data[i].value;
    }
    if (data.type === 'D') {
        finalArray.push({
        month: data[i].date
        A: valueA,
        B: valueB,
        C: valueC,
        D: data[i].value
        });
    }
}
console.log(finalArray);

让我知道它是否有效。

答案 2 :(得分:0)

鉴于您的输入是

let input = {
        "data": [
            {
                "type": "A",
                "date": "2018-05",
                "value": "153"
            },
            {
                "type": "B",
                "date": "2018-05",
                "value": "888"
            },
            {
                "type": "C",
                "date": "2018-05",
                "value": "999"
            },
            {
                "type": "D",
                "date": "2018-05",
                "value": "555"
            },
            {
                "type": "A",
                "date": "2018-06",
                "value": "148"
            },
            {
                "type": "B",
                "date": "2018-06",
                "value": "222"
            },
            {
                "type": "C",
                "date": "2018-06",
                "value": "555"
            },
            {
                "type": "D",
                "date": "2018-06",
                "value": "666"
            },
            {
                "type": "A",
                "date": "2018-07",
                "value": "156"
            },
            {
                "type": "B",
                "date": "2018-07",
                "value": "111"
            },
            {
                "type": "C",
                "date": "2018-07",
                "value": "333"
            },
            {
                "type": "D",
                "date": "2018-07",
                "value": "999"
            }
        ],
        "number": "111-111"
    };

您可以执行此操作以在output变量中获得所需的结果

let output = [];

    input.data.forEach(function (item) {
        const type = item.type;
        const date = item.date;
        const value = item.value;

        let newlyCreated = false;
        let objForMonth = output.find(x => x.month == date);

        if (!objForMonth) {
            newlyCreated = true;
            objForMonth = {};
            objForMonth.month = date;
        }
        objForMonth[type] = value;

        if (newlyCreated) {
            output.push(objForMonth);
        } else {
            let indexToWriteTo = output.findIndex(x => x.month == date);
            output[indexToWriteTo] = objForMonth;
        }
    });