我有一个要格式化的JSON以呈现html表
Array
的JSON,里面有Objects
billdatewise
和outletwise
的数据我的代码
var cpy = [{
"billdate": "2018-09-02",
"outlet": "JAYANAGAR",
"Total": 511153,
"cash": 490289,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 20864,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-09-02",
"outlet": "MALLESHWARAM",
"Total": 115704,
"cash": 112854,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 2850,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-09-02",
"outlet": "KOLAR",
"Total": 83597,
"cash": 83597,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 0,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-09-03",
"outlet": "JAYANAGAR",
"Total": 167421,
"cash": 167421,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 0,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-09-03", //here for billdate 2018-09-03 i don't have data for malleshwaram so i want to have output as 0 when i am formating the json
"outlet": "KOLAR",
"Total": 53775,
"cash": 53775,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 0,
"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) {
console.log("billdarte :-" + obj.billdate)
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]
})
arr.push(row)
})
})
return arr;
}
var tableValue = structureJSON(getDataList(cpy))
console.log(tableValue)
关键点
billdate
2018-09-02至2018-09-03的数据outlets
都在其中,即Jayanagar
,Malleshwaram
和Kolar
Malleshwaram
没有销售,因此帐单日期2018-09-03缺少malleshwaram
的对象我要尝试的是,当该日期没有任何数据可用于出口时,该对象应显示为0
我的预期输出是
[
{
"billdate": "2018-09-02",
"SalesType": "Total",
"JAYANAGAR": "511153",
"MALLESHWARAM": "115704",
"KOLAR": "83597"
},
{
"billdate": "",
"SalesType": "cash",
"JAYANAGAR": "490289",
"MALLESHWARAM": "112854",
"KOLAR": "83597"
},
{
"billdate": "",
"SalesType": "creditcard",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "coupon",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "paytm",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "credit",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "swiggy",
"JAYANAGAR": "20864",
"MALLESHWARAM": "2850",
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "kb",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "bigbasket",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",
"KOLAR": "0"
},
{
"billdate": "2018-09-03",
"SalesType": "Total",
"JAYANAGAR": "167421",
"MALLESHWARAM": "0", // this malleshwaram should be 0
"KOLAR": "53775"
},
{
"billdate": "",
"SalesType": "cash",
"JAYANAGAR": "167421",
"MALLESHWARAM": "0",// this malleshwaram should be 0
"KOLAR": "53775"
},
{
"billdate": "",
"SalesType": "creditcard",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",// this malleshwaram should be 0
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "coupon",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",// this malleshwaram should be 0
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "paytm",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",// this malleshwaram should be 0
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "credit",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",// this malleshwaram should be 0
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "swiggy",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",// this malleshwaram should be 0
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "kb",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",// this malleshwaram should be 0
"KOLAR": "0"
},
{
"billdate": "",
"SalesType": "bigbasket",
"JAYANAGAR": "0",
"MALLESHWARAM": "0",// this malleshwaram should be 0
"KOLAR": "0"
}
]
到目前为止,我的脑海中是我在解析时必须检查obj.Total
像obj.Total?obj.Total:0
但不知道如何实现
我已经完全解释了我的问题,如果有任何疑问,请问我,我从现在开始一直被困在这里
我已经在创建唯一的outlets
请任何人帮助
答案 0 :(得分:0)
好,所以我认为这就是您想要的:
function countOutlets(obj)
{
let objUnique= obj.outlet.filter(function(item, index){
return obj.outlet.indexOf(item) >= index;
});
return objUnique
}
var cpy = [{
"billdate": "2018-09-02",
"outlet": "JAYANAGAR",
"Total": 511153,
"cash": 490289,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 20864,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-09-02",
"outlet": "MALLESHWARAM",
"Total": 115704,
"cash": 112854,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 2850,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-09-02",
"outlet": "KOLAR",
"Total": 83597,
"cash": 83597,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 0,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-09-03",
"outlet": "JAYANAGAR",
"Total": 167421,
"cash": 167421,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 0,
"kb": 0,
"bigbasket": 0
},
{
"billdate": "2018-09-03", //here for billdate 2018-09-03 i don't have data for malleshwaram so i want to have output as 0 when i am formating the json
"outlet": "KOLAR",
"Total": 53775,
"cash": 53775,
"creditcard": 0,
"coupon": 0,
"paytm": 0,
"credit": 0,
"swiggy": 0,
"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) {
let alloutlets=countOutlets(getDataList(cpy))
console.log("billdarte :-" + obj.billdate)
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
};
}
alloutlets.forEach((ele2, index2) => {
var selected = cpy.filter((ele3, index3) => {
return ele3.billdate === ele1 && ele3.outlet == ele2
});
if(selected.length!=0)
row[ele2] = selected[0][ele]
else
row[ele2] = "0";
})
arr.push(row)
})
})
return arr;
}
var tableValue = structureJSON(getDataList(cpy))
console.log(tableValue)
我意识到我的countOutlets有点多余,但是到我意识到时,我已经做到了。
希望这会有所帮助。