我有一个JSON,我正在尝试通过.map
函数进行转换
但是我得到了错误的结果
因为我有一个Array
,并且里面有几个Objects
,但是格式化数据后,我得到了Array
,里面是Array
然后是Object
< / p>
我当前的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)));
我要获取的所有数据都很完美
{
"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))
请检查我的编辑部分
答案 0 :(得分:1)
将其分解为不同的功能,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));