Javascript 将json响应转换为表

Javascript 将json响应转换为表,javascript,json,typescript,Javascript,Json,Typescript,我试图将json响应转换为接受我的表(来自devextreme的组件)的数组。我正试着在6号上做这个。Json看起来像: "data": [ { "type": "A", "date": "2018-05", "value": "153" }, { "type": "B",

我试图将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)。有人能帮我吗?我正在使用此组件在网站上显示数据

您可以这样尝试

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);
让finalArray=[];
让valueA:string=null;
让valueB:string=null;
让valueC:string=null;
//这里的数据是一个包含JSON数据的变量
for(设i=0;i
让我知道它是否工作。

你可以这样试试

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);
让finalArray=[];
让valueA:string=null;
让valueB:string=null;
让valueC:string=null;
//这里的数据是一个包含JSON数据的变量
for(设i=0;i

让我知道它是否工作。

这是我解决您问题的方法:

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

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

2) 一旦有了这两个数组,我们将遍历原始列表以生成对象。属性
A
B
C
D
的值通过过滤原始
列表中的
日期和
类型来填充

const list={“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”、“价值”:“666”}、{“类型”:“A”、“日期”:“2018-07”、“价值”:“156”}、{“类型”:“B”、“日期”:“2018-07”、“价值”:“111”}、{“类型”:“C”、“日期”:“2018-07”、“价值”:“333”}、{“类型”:“D”、“日期”:“2018-07”、“价值”:“999”}、“编号”:“111”};
//const dates=[…新集合(list.data.map(item=>item.date));
const dates=Array.from(list.data.map(item=>item.date));
控制台日志(日期);
//consttypes=[…新集合(list.data.map(item=>item.type));
consttypes=Array.from(list.data.map(item=>item.type));
console.log(类型)
const res=dates.map(日期=>{
常量obj={};
types.map(type=>{
obj[type]=list.data.filter(item=>item.date==date&&item.type==type)[0]。值;
});
obj.date=日期;
返回obj;
});

console.log(res);
以下是我解决您问题的方法:

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

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

2) 一旦有了这两个数组,我们将遍历原始列表以生成对象。通过过滤原始
列表中的
日期
类型
来填充属性
A
B
C
D
的值

const list={“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“,”价值“:”666“,”类型“,”日期“:”2018-07“,”价值“:”156“,”类型“,”日期“:”2018-07“,”价值“:”111“,”类型“,”C“,”日期“:”2018-07“,”价值“:”333“,”日期“:”2018-07“,”价值“:”999“,”数字“:”111-111“;
//const dates=[…新集合(list.data.map(item=>item.date));
const dates=Array.from(list.data.map(item=>item.date));
控制台日志(日期);
//consttypes=[…新集合(list.data.map(item=>item.type));
consttypes=Array.from(list.data.map(item=>item.type));
console.log(类型)
const res=dates.map(日期=>{
常量obj={};
types.map(type=>{
obj[type]=list.data.filter(item=>item.date==date&&item.type==type)[0]。值;
});
obj.date=日期;
返回obj;
});

console.log(res);
假设您的输入是

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"
    };
您可以这样做以在
输出
变量中获得所需的结果

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;
        }
    });

鉴于您的输入是

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"
    };
您可以这样做以在
输出
变量中获得所需的结果

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;
        }
    });

我认为在数据(data[I].type)旁边应该有[I]。这是一个很好的答案,但我没有提到类型的数量可以改变(例如,可能只有A、B和C)对不起:(.是的,我的坏应该是我
I
A