如何在打字稿中格式化来自http get Request的结果

时间:2019-11-14 07:45:33

标签: json angular http ag-grid

我有一个角度项目,我在其中使用了一个API,该API以这种格式将数据发送回去。 Head是包含用“,”分隔的所有键/属性的字符串。 我想在ag-grid

中显示数据
   {
"Head": "Sample Number,Insurance Type,Insurance Type Name,Quote Description,NVIC,Car Detail,GNAF,Search Address,Gender,Borrowing Amount,Deposit_Amount,Loan_Term,Residual,Own_Property,Credit_Score,Vehicle_Type,Annual_Income,Loan_Type,Loan_Interest_type,Seller_Type,Error Message",
"Items": [
    "1,FCL,Finance Car Loan,Sample,MX313A,,GANT_702976160,,Male,13107,2000,5,0,No,621,Used,73200,Secured,Fixed,Private,NVIC not found.",
    "2,FCL,Finance Car Loan,Sample,N1J13A,,GANT_716848802,,Female,10630,0,5,0,No,512,Used,39600,Secured,Fixed,Private,NVIC not found.",
    "3,FCL,Finance Car Loan,Sample,01JZ18,,GANT_717255160,,Male,11429,2000,5,0,Yes,662,New,64188,Secured,Fixed,Dealer,NVIC not found.",
    "4,FCL,Finance Car Loan,Sample,MWE13A,,GANSW710285256,,Female,14483,3500,4,0,No,350,Used,40800,Secured,Fixed,Private,NVIC not found.",
}

如何格式化来自API的响应,例如{property1:value,property2:value ....} 还是有一种方法可以将相同格式的数据用于ag-grid

2 个答案:

答案 0 :(得分:1)

尝试一下

const data = {
      "Head": "Sample Number,Insurance Type,Insurance Type Name,Quote Description,NVIC,Car Detail,GNAF,Search Address,Gender,Borrowing Amount,Deposit_Amount,Loan_Term,Residual,Own_Property,Credit_Score,Vehicle_Type,Annual_Income,Loan_Type,Loan_Interest_type,Seller_Type,Error Message",
      "Items": [
          "1,FCL,Finance Car Loan,Sample,MX313A,,GANT_702976160,,Male,13107,2000,5,0,No,621,Used,73200,Secured,Fixed,Private,NVIC not found.",
          "2,FCL,Finance Car Loan,Sample,N1J13A,,GANT_716848802,,Female,10630,0,5,0,No,512,Used,39600,Secured,Fixed,Private,NVIC not found.",
          "3,FCL,Finance Car Loan,Sample,01JZ18,,GANT_717255160,,Male,11429,2000,5,0,Yes,662,New,64188,Secured,Fixed,Dealer,NVIC not found.",
          "4,FCL,Finance Car Loan,Sample,MWE13A,,GANSW710285256,,Female,14483,3500,4,0,No,350,Used,40800,Secured,Fixed,Private,NVIC not found.",
      ]
    };
const keys: string[] = data.Head.split(',');
const items = data.Items.map(item => lodash.zipObject(keys, item.split(','))); // import * as lodash from 'lodash';

结果为

[{"Sample Number":"1","Insurance Type":"FCL","Insurance Type Name":"Finance Car Loan","Quote Description":"Sample","NVIC":"MX313A","Car Detail":"","GNAF":"GANT_702976160","Search Address":"","Gender":"Male","Borrowing Amount":"13107","Deposit_Amount":"2000","Loan_Term":"5","Residual":"0","Own_Property":"No","Credit_Score":"621","Vehicle_Type":"Used","Annual_Income":"73200","Loan_Type":"Secured","Loan_Interest_type":"Fixed","Seller_Type":"Private","Error Message":"NVIC not found."},{"Sample Number":"2","Insurance Type":"FCL","Insurance Type Name":"Finance Car Loan","Quote Description":"Sample","NVIC":"N1J13A","Car Detail":"","GNAF":"GANT_716848802","Search Address":"","Gender":"Female","Borrowing Amount":"10630","Deposit_Amount":"0","Loan_Term":"5","Residual":"0","Own_Property":"No","Credit_Score":"512","Vehicle_Type":"Used","Annual_Income":"39600","Loan_Type":"Secured","Loan_Interest_type":"Fixed","Seller_Type":"Private","Error Message":"NVIC not found."},{"Sample Number":"3","Insurance Type":"FCL","Insurance Type Name":"Finance Car Loan","Quote Description":"Sample","NVIC":"01JZ18","Car Detail":"","GNAF":"GANT_717255160","Search Address":"","Gender":"Male","Borrowing Amount":"11429","Deposit_Amount":"2000","Loan_Term":"5","Residual":"0","Own_Property":"Yes","Credit_Score":"662","Vehicle_Type":"New","Annual_Income":"64188","Loan_Type":"Secured","Loan_Interest_type":"Fixed","Seller_Type":"Dealer","Error Message":"NVIC not found."},{"Sample Number":"4","Insurance Type":"FCL","Insurance Type Name":"Finance Car Loan","Quote Description":"Sample","NVIC":"MWE13A","Car Detail":"","GNAF":"GANSW710285256","Search Address":"","Gender":"Female","Borrowing Amount":"14483","Deposit_Amount":"3500","Loan_Term":"4","Residual":"0","Own_Property":"No","Credit_Score":"350","Vehicle_Type":"Used","Annual_Income":"40800","Loan_Type":"Secured","Loan_Interest_type":"Fixed","Seller_Type":"Private","Error Message":"NVIC not found."}]

答案 1 :(得分:0)

例如,您可以尝试强大的还原功能

let someObject = {
  "Head": "Sample Number,Insurance Type,Insurance Type Name,Quote Description,NVIC,Car Detail,GNAF,Search Address,Gender,Borrowing Amount,Deposit_Amount,Loan_Term,Residual,Own_Property,Credit_Score,Vehicle_Type,Annual_Income,Loan_Type,Loan_Interest_type,Seller_Type,Error Message",
  "Items": [
    "1,FCL,Finance Car Loan,Sample,MX313A,,GANT_702976160,,Male,13107,2000,5,0,No,621,Used,73200,Secured,Fixed,Private,NVIC not found.",
    "2,FCL,Finance Car Loan,Sample,N1J13A,,GANT_716848802,,Female,10630,0,5,0,No,512,Used,39600,Secured,Fixed,Private,NVIC not found.",
    "3,FCL,Finance Car Loan,Sample,01JZ18,,GANT_717255160,,Male,11429,2000,5,0,Yes,662,New,64188,Secured,Fixed,Dealer,NVIC not found.",
    "4,FCL,Finance Car Loan,Sample,MWE13A,,GANSW710285256,,Female,14483,3500,4,0,No,350,Used,40800,Secured,Fixed,Private,NVIC not found.",]
}
someObject.Head = someObject.Head.split(',');
someObject.Items = someObject.Items.map(item => item.split(',')).flat();

let result = someObject.Head.reduce(function (acc, item, i) {
  return Object.assign(acc, { [item]: someObject.Items[i] })
}, {});

console.log(result);