简体   繁体   English

格式化 JSON 时遇到问题

[英]facing issue while formating the JSON

I have a JSON which i am trying to convert as i want through .map functions But i am getting wrong result我有一个 JSON,我正在尝试通过.map函数按我想要的方式进行转换,但我得到了错误的结果

As i have a Array and inside that i have several Objects but after formatting the data i am getting Array inside which Array then Object因为我有一个Array ,里面有几个Objects但在格式化数据后,我得到了Array ,其中Array然后是Object

My current json我目前的 json

var cpy = [
      {
            "billdate": "2018-08-01",
            "outlet": "S0001",
            "amount": 10,
            "cash": 5,
            "creditcard": 0,
            "coupon": 0,
            "paytm": 0,
            "credit": 0,
            "swiggy": 5,
            "kb": 0,
            "bigbasket": 0
          },
          {
            "billdate": "2018-08-01",
            "outlet": "S0002",
            "amount": 10,
            "cash": 5,
            "creditcard": 0,
            "coupon": 0,
            "paytm": 0,
            "credit": 0,
            "swiggy": 5,
            "kb": 0,
            "bigbasket": 0
          },
          {
            "billdate": "2018-08-01",
            "outlet": "S0009",
            "amount": 11,
            "cash": 6,
            "creditcard": 0,
            "coupon": 0,
            "paytm": 0,
            "credit": 0,
            "swiggy": 5,
            "kb": 0,
            "bigbasket": 0
          },
          {
            "billdate": "2018-08-02",
            "outlet": "S0001",
            "amount": 10,
            "cash": 0,
            "creditcard": 0,
            "coupon": 0,
            "paytm": 0,
            "credit": 0,
            "swiggy": 55,
            "kb": 0,
            "bigbasket": 0
          },
          {
            "billdate": "2018-08-02",
            "outlet": "S0002",
            "amount": 15,
            "cash": 55034,
            "creditcard": 0,
            "coupon": 0,
            "paytm": 0,
            "credit": 0,
            "swiggy": 5,
            "kb": 0,
            "bigbasket": 0
          },
          {
            "billdate": "2018-08-02",
            "outlet": "S0009",
            "amount": 10,
            "cash": 72213,
            "creditcard": 0,
            "coupon": 0,
            "paytm": 0,
            "credit": 0,
            "swiggy": 10,
            "kb": 0,
            "bigbasket": 0
          }
        ]

from the above i am trying to get like below从上面我想像下面一样

[
    {
        "billdate": "",
        "sales type": "Full total",        //this one is the header
        "S001": "20",  //full total of amount of outlet `S001` date wise i.e for 2018-08-01 and 2018-08-02
        "S002": "30",   //full total of amount of outlet `S002` date wise i.e for 2018-08-01 and 2018-08-02
        "S009": "33"     //full total of amount of outlet `S003` date wise i.e for 2018-08-01 and 2018-08-02
    },
    {
        "billdate": "2018-08-01",
        "sales type": "Totals",
        "S001": "10",
        "S002": "10",
        "S009": "11"
    },
    {
        "billdate": "",
        "sales type": " CASH",
        "S001": "5",
        "S002": "5",
        "S009": "6"
    },
    {
        "billdate": "",
        "sales type": " CREDIT CARD",
        "S001": "0",
        "S002": "0",
        "S009": "0"
    },
    {
        "billdate": "",
        "sales type": "coupon",
        "S001": "0",
        "S002": "0",
        "S009": "0"
    },
    {
        "billdate": "",
        "sales type": "paytm",
        "S001": "0",
        "S002": "0",
        "S009": "0"
    },
    {
        "billdate": "",
        "sales type": "swiggy",
        "S001": "5",
        "S002": "5",
        "S009": "5"
    },
    {
        "billdate": "2018-08-02",
        "sales type": "Totals",
        "S001": "10",
        "S002": "20",
        "S009": "20"
    },
    {
        "billdate": "",
        "sales type": " CASH",
        "S001": "5",
        "S002": "15",
        "S009": "10"
    },
    {
        "billdate": "",
        "sales type": " CREDIT CARD",
        "S001": "0",
        "S002": "0",
        "S009": "0"
    },
    {
        "billdate": "",
        "sales type": "coupon",
        "S001": "0",
        "S002": "0",
        "S009": "0"
    },
    {
        "billdate": "",
        "sales type": "paytm",
        "S001": "0",
        "S002": "0",
        "S009": "0"
    },
    {
        "billdate": "",
        "sales type": "swiggy",
        "S001": "5",
        "S002": "5",
        "S009": "10"
    }

]

but when i am running my code firstly for some loops i am getting data as it is there then something like i want但是当我首先为某些循环运行我的代码时,我正在获取数据,因为它在那里然后像我想要的那样

Here is what i have done这是我所做的

 var cpy = [{ "billdate": "2018-08-01", "outlet": "S0001", "amount": 10, "cash": 5, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 5, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-01", "outlet": "S0002", "amount": 10, "cash": 5, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 5, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-01", "outlet": "S0009", "amount": 11, "cash": 6, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 5, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-02", "outlet": "S0001", "amount": 10, "cash": 0, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 55, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-02", "outlet": "S0002", "amount": 15, "cash": 55034, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 5, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-02", "outlet": "S0009", "amount": 10, "cash": 72213, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 10, "kb": 0, "bigbasket": 0 } ] function getDataList(jsn) { var billdate = []; var outlet = []; var salesType = []; billdate = [...new Set(jsn.map(ele => ele.billdate))]; outlet = [...new Set(jsn.map(ele => ele.outlet))]; for (let i in jsn[0]) { if (i !== 'billdate' && i !== 'outlet') { salesType.push(i) } } return { billdate, outlet, salesType } } function structureJSON(obj) { var arr = []; obj.billdate.forEach((ele1, index1) => { obj.salesType.forEach((ele, index) => { let row; if (index === 0) { row = { billdate: ele1, selesType: ele }; } else { row = { billdate: "", selesType: ele }; } obj.outlet.forEach((ele2, index2) => { var selected = cpy.filter((ele3, index3) => { return ele3.billdate === ele1 && ele3.outlet === ele2 }); row[ele2] = selected[0][ele] }) arr.push(row) }) }) return arr; } console.log(structureJSON(getDataList(cpy)));
just want to add the header which can calculate the full total like in the json i wanted 只想添加可以像我想要的json一样计算全部总数的标题

all the data i am getting is perfect just want to add我得到的所有数据都是完美的只是想添加

{ "billdate": "", "sales type": "Full total", //this one is the header "S001": "20", //full total of amount of outlet `S001` date wise ie for 2018-08-01 and 2018-08-02 "S002": "30", //full total of amount of outlet `S002` date wise ie for 2018-08-01 and 2018-08-02 "S009": "33" //full total of amount of outlet `S003` date wise ie for 2018-08-01 and 2018-08-02 },

Edit编辑

as i am using different json of same type but values are different it is showing error as typeerrorselected[0] is undefined由于我使用相同类型的不同 json 但值不同,因此显示错误,因为typeerrorselected[0] is undefined

 var cpy = [{ "billdate": "2018-09-01", "outlet": "JAYANAGAR", "amount": 476426, "cash": 465818, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 10608, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-01", "outlet": "MALLESHWARAM", "amount": 92141, "cash": 87903, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 4238, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-01", "outlet": "KOLAR", "amount": 115313, "cash": 115313, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 0, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-02", "outlet": "JAYANAGAR", "amount": 511153, "cash": 490289, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 20864, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-02", "outlet": "MALLESHWARAM", "amount": 115704, "cash": 112854, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 2850, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-02", "outlet": "KOLAR", "amount": 83597, "cash": 83597, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 0, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-03", "outlet": "JAYANAGAR", "amount": 167421, "cash": 167421, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 0, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-03", "outlet": "KOLAR", "amount": 53775, "cash": 53775, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 0, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-04", "outlet": "JAYANAGAR", "amount": 269711, "cash": 264363, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 5348, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-04", "outlet": "MALLESHWARAM", "amount": 58849, "cash": 56865, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 1984, "kb": 0, "bigbasket": 0, "zomato": 0 }, { "billdate": "2018-09-04", "outlet": "KOLAR", "amount": 82998, "cash": 82998, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 0, "kb": 0, "bigbasket": 0, "zomato": 0 } ] function getDataList(jsn) { var billdate = []; var outlet = []; var SalesType = []; billdate = [...new Set(jsn.map(ele => ele.billdate))]; outlet = [...new Set(jsn.map(ele => ele.outlet))]; for (let i in jsn[0]) { if (i !== 'billdate' && i !== 'outlet') { SalesType.push(i) } } return { billdate, outlet, SalesType } } function structureJSON(obj) { var arr = []; obj.billdate.forEach((ele1, index1) => { obj.SalesType.forEach((ele, index) => { let row; if (index === 0) { row = { billdate: ele1, SalesType: ele }; } else { row = { billdate: "", SalesType: ele }; } obj.outlet.forEach((ele2, index2) => { var selected = cpy.filter((ele3, index3) => { return ele3.billdate === ele1 && ele3.outlet === ele2 }); row[ele2] = selected[0][ele] //here it is showing error console.log(row[ele2]) }) arr.push(row) }) }) return arr; } var tableValue = structureJSON(getDataList(cpy))

please check my edited part请检查我编辑的部分

Would break this down into different functions, getOutletTotals would look something like this:将其分解为不同的函数,getOutletTotals 将如下所示:

 var cpy = [{ "billdate": "2018-08-01", "outlet": "S0001", "amount": 10, "cash": 5, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 5, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-01", "outlet": "S0002", "amount": 10, "cash": 5, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 5, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-01", "outlet": "S0009", "amount": 11, "cash": 6, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 5, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-02", "outlet": "S0001", "amount": 10, "cash": 0, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 55, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-02", "outlet": "S0002", "amount": 15, "cash": 55034, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 5, "kb": 0, "bigbasket": 0 }, { "billdate": "2018-08-02", "outlet": "S0009", "amount": 10, "cash": 72213, "creditcard": 0, "coupon": 0, "paytm": 0, "credit": 0, "swiggy": 10, "kb": 0, "bigbasket": 0 } ] function getOutletTotals(data) { const totals = {}; data.forEach(item => { if (totals.hasOwnProperty(item.outlet)) { // Add to existing outlet total totals[item.outlet] += item.amount; } else { // First time we've seen this outlet totals[item.outlet] = item.amount; } }); totals['billdate'] = ""; totals['sales type'] = "Full total"; return totals; } console.log(getOutletTotals(cpy));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM