首页 > 解决方案 > 格式化 JSON 时遇到问题

问题描述

我有一个 JSON,我正在尝试通过.map函数进行转换,但我得到了错误的结果

因为我有一个Array和里面我有几个Objects但是在格式化数据之后我正在Array里面Array然后Object

我当前的 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
          }
        ]

从上面我试图得到如下

[
    {
        "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"
    }

]

但是当我首先为一些循环运行我的代码时,我正在获取数据,因为它在那里,然后是我想要的东西

这是我所做的

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)));
只想添加可以像我想要的json一样计算全部总数的标题

我得到的所有数据都很完美只是想添加

{
        "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
    },

编辑

因为我使用相同类型的不同 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))

请检查我编辑的部分

标签: javascriptjson

解决方案


将其分解为不同的函数,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));


推荐阅读