gpt4 book ai didi

javascript - 格式化 JSON 时遇到问题

转载 作者:行者123 更新时间:2023-11-29 23:05:13 24 4
gpt4 key购买 nike

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

因为我有一个 Array 并且在里面我有几个 Objects 但是在格式化数据之后我得到了 Array 其中 数组然后是对象

我当前的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 中那样计算完整总数的 header

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

{
"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))

请检查我编辑的部分

最佳答案

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

关于javascript - 格式化 JSON 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54918890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com