I am trying to summarize a JSON,I have tried several example from Stack Overflow
How to get max of in array of objects
How to make a foreach of an object in javascript?
but none of the tries have been successful.
Sale: [
{
saleID: "2",
timeStamp: "2021-01-08T17:50:28+00:00",
discountPercent: "0",
completed: "true",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-08T17:49:53+00:00",
updateTime: "2021-01-08T17:50:29+00:00",
completeTime: "2021-01-08T17:50:28+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "500",
ticketNumber: "220000000002",
calcDiscount: "0",
calcTotal: "500",
calcSubtotal: "416.67",
calcTaxable: "416.67",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "83.33",
calcTax2: "0",
calcPayments: "500",
total: "500",
totalDue: "500",
displayableTotal: "500",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "83.33"
},
{
saleID: "3",
timeStamp: "2021-01-08T17:53:18+00:00",
discountPercent: "0",
completed: "false",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-08T17:53:18+00:00",
updateTime: "2021-01-08T17:53:18+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "0",
ticketNumber: "220000000003",
calcDiscount: "0",
calcTotal: "0",
calcSubtotal: "0",
calcTaxable: "0",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "0",
calcTax2: "0",
calcPayments: "0",
total: "0",
totalDue: "0",
displayableTotal: "0",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "0"
},
{
saleID: "5",
timeStamp: "2021-01-08T17:54:43+00:00",
discountPercent: "0",
completed: "false",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-08T17:54:43+00:00",
updateTime: "2021-01-08T17:54:43+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "0",
ticketNumber: "220000000005",
calcDiscount: "0",
calcTotal: "0",
calcSubtotal: "0",
calcTaxable: "0",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "0",
calcTax2: "0",
calcPayments: "0",
total: "0",
totalDue: "0",
displayableTotal: "0",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "0"
},
{
saleID: "6",
timeStamp: "2021-01-24T18:49:27+00:00",
discountPercent: "0",
completed: "true",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-24T18:48:30+00:00",
updateTime: "2021-01-24T18:49:28+00:00",
completeTime: "2021-01-24T18:49:27+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "316.69",
ticketNumber: "220000000006",
calcDiscount: "0",
calcTotal: "316.69",
calcSubtotal: "263.91",
calcTaxable: "263.91",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "52.78",
calcTax2: "0",
calcPayments: "316.69",
total: "316.69",
totalDue: "316.69",
displayableTotal: "316.69",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "52.78"
}]
I have tried several ways, the latest one:
const debitSummary = sales.reduce((acc, inv, i) => {
// console.log("inside reduce");
if (i) {
// if (parseFloat(inv.calcTotal>0)){ // invoice
acc = {
//...acc,
ticketNumber: acc.ticketNumber + ',' + inv.ticketNumber,
registerID: inv.registerID > acc.registerID ? inv.registerID : acc.registerID,
calcTotal: parseFloat(acc.calcTotal) + parseFloat(inv.calcTotal),
timeStamp: formatDate(inv.timeStamp) > formatDate(acc.timeStamp) ? formatDate(inv.timeStamp) : formatDate(acc.timeStamp),
}
// }
}
return acc;
},sales[0] );
But impossible to achieve the goal:
sum of calcTotal,
sum of total,
array of saleID,
array of ticketNumber,
with a break by registerID and day of timeStamp:
[{
calcTotal: "500",
registerID: "1",
saleID: {
"2",
"3",
"5"
},
ticketNumber: {
"220000000002",
"220000000003",
"220000000005"
},
timeStamp: "2021-01-08",
total: "500"
},
{
calcTotal: "316.69",
registerID: "1",
saleID: "6",
ticketNumber: {
"220000000006"
},
timeStamp: "2021-01-24",
total: "316.69"
}
]
You could take an object with date part of the timestamp as key and get the values from the object as result.
const
sale = [{ saleID: "2", timeStamp: "2021-01-08T17:50:28+00:00", discountPercent: "0", completed: "true", archived: "false", voided: "false", enablePromotions: "true", isTaxInclusive: "true", createTime: "2021-01-08T17:49:53+00:00", updateTime: "2021-01-08T17:50:29+00:00", completeTime: "2021-01-08T17:50:28+00:00", referenceNumber: "", referenceNumberSource: "", tax1Rate: "0.2", tax2Rate: "0", change: "0", receiptPreference: "printed", displayableSubtotal: "500", ticketNumber: "220000000002", calcDiscount: "0", calcTotal: "500", calcSubtotal: "416.67", calcTaxable: "416.67", calcNonTaxable: "0", calcAvgCost: "0", calcFIFOCost: "0", calcTax1: "83.33", calcTax2: "0", calcPayments: "500", total: "500", totalDue: "500", displayableTotal: "500", balance: "0", customerID: "0", discountID: "0", employeeID: "1", quoteID: "0", registerID: "1", shipToID: "0", shopID: "1", taxCategoryID: "1", taxTotal: "83.33" }, { saleID: "3", timeStamp: "2021-01-08T17:53:18+00:00", discountPercent: "0", completed: "false", archived: "false", voided: "false", enablePromotions: "true", isTaxInclusive: "true", createTime: "2021-01-08T17:53:18+00:00", updateTime: "2021-01-08T17:53:18+00:00", referenceNumber: "", referenceNumberSource: "", tax1Rate: "0.2", tax2Rate: "0", change: "0", receiptPreference: "printed", displayableSubtotal: "0", ticketNumber: "220000000003", calcDiscount: "0", calcTotal: "0", calcSubtotal: "0", calcTaxable: "0", calcNonTaxable: "0", calcAvgCost: "0", calcFIFOCost: "0", calcTax1: "0", calcTax2: "0", calcPayments: "0", total: "0", totalDue: "0", displayableTotal: "0", balance: "0", customerID: "0", discountID: "0", employeeID: "1", quoteID: "0", registerID: "1", shipToID: "0", shopID: "1", taxCategoryID: "1", taxTotal: "0" }, { saleID: "5", timeStamp: "2021-01-08T17:54:43+00:00", discountPercent: "0", completed: "false", archived: "false", voided: "false", enablePromotions: "true", isTaxInclusive: "true", createTime: "2021-01-08T17:54:43+00:00", updateTime: "2021-01-08T17:54:43+00:00", referenceNumber: "", referenceNumberSource: "", tax1Rate: "0.2", tax2Rate: "0", change: "0", receiptPreference: "printed", displayableSubtotal: "0", ticketNumber: "220000000005", calcDiscount: "0", calcTotal: "0", calcSubtotal: "0", calcTaxable: "0", calcNonTaxable: "0", calcAvgCost: "0", calcFIFOCost: "0", calcTax1: "0", calcTax2: "0", calcPayments: "0", total: "0", totalDue: "0", displayableTotal: "0", balance: "0", customerID: "0", discountID: "0", employeeID: "1", quoteID: "0", registerID: "1", shipToID: "0", shopID: "1", taxCategoryID: "1", taxTotal: "0" }, { saleID: "6", timeStamp: "2021-01-24T18:49:27+00:00", discountPercent: "0", completed: "true", archived: "false", voided: "false", enablePromotions: "true", isTaxInclusive: "true", createTime: "2021-01-24T18:48:30+00:00", updateTime: "2021-01-24T18:49:28+00:00", completeTime: "2021-01-24T18:49:27+00:00", referenceNumber: "", referenceNumberSource: "", tax1Rate: "0.2", tax2Rate: "0", change: "0", receiptPreference: "printed", displayableSubtotal: "316.69", ticketNumber: "220000000006", calcDiscount: "0", calcTotal: "316.69", calcSubtotal: "263.91", calcTaxable: "263.91", calcNonTaxable: "0", calcAvgCost: "0", calcFIFOCost: "0", calcTax1: "52.78", calcTax2: "0", calcPayments: "316.69", total: "316.69", totalDue: "316.69", displayableTotal: "316.69", balance: "0", customerID: "0", discountID: "0", employeeID: "1", quoteID: "0", registerID: "1", shipToID: "0", shopID: "1", taxCategoryID: "1", taxTotal: "52.78" }],
result = Object.values(sale.reduce((r, o) => {
const timeStamp = o.timeStamp.slice(0, 10);
r[timeStamp] ??= { timeStamp, registerID: o.registerID, ticketNumber: [], saleID: [], calcTotal: 0, total: 0 };
['calcTotal', 'total'].forEach(k => r[timeStamp][k] += +o[k]);
['saleID', 'ticketNumber'].forEach(k => r[timeStamp][k].push(o[k]));
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
j = [
{
saleID: "2",
timeStamp: "2021-01-08T17:50:28+00:00",
discountPercent: "0",
completed: "true",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-08T17:49:53+00:00",
updateTime: "2021-01-08T17:50:29+00:00",
completeTime: "2021-01-08T17:50:28+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "500",
ticketNumber: "220000000002",
calcDiscount: "0",
calcTotal: "500",
calcSubtotal: "416.67",
calcTaxable: "416.67",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "83.33",
calcTax2: "0",
calcPayments: "500",
total: "500",
totalDue: "500",
displayableTotal: "500",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "83.33"
},
{
saleID: "3",
timeStamp: "2021-01-08T17:53:18+00:00",
discountPercent: "0",
completed: "false",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-08T17:53:18+00:00",
updateTime: "2021-01-08T17:53:18+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "0",
ticketNumber: "220000000003",
calcDiscount: "0",
calcTotal: "0",
calcSubtotal: "0",
calcTaxable: "0",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "0",
calcTax2: "0",
calcPayments: "0",
total: "0",
totalDue: "0",
displayableTotal: "0",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "0"
},
{
saleID: "5",
timeStamp: "2021-01-08T17:54:43+00:00",
discountPercent: "0",
completed: "false",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-08T17:54:43+00:00",
updateTime: "2021-01-08T17:54:43+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "0",
ticketNumber: "220000000005",
calcDiscount: "0",
calcTotal: "0",
calcSubtotal: "0",
calcTaxable: "0",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "0",
calcTax2: "0",
calcPayments: "0",
total: "0",
totalDue: "0",
displayableTotal: "0",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "0"
},
{
saleID: "6",
timeStamp: "2021-01-24T18:49:27+00:00",
discountPercent: "0",
completed: "true",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-24T18:48:30+00:00",
updateTime: "2021-01-24T18:49:28+00:00",
completeTime: "2021-01-24T18:49:27+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "316.69",
ticketNumber: "220000000006",
calcDiscount: "0",
calcTotal: "316.69",
calcSubtotal: "263.91",
calcTaxable: "263.91",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "52.78",
calcTax2: "0",
calcPayments: "316.69",
total: "316.69",
totalDue: "316.69",
displayableTotal: "316.69",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "52.78"
}];
result = j.reduce((r, { registerID,timeStamp, ...object }) => {
var temp = r.find(o => o.registerID === registerID && (new Date(o.timeStamp)).toLocaleDateString() === (new Date(timeStamp)).toLocaleDateString());
if (!temp) r.push(temp = { calcTotal: "0", registerID,timeStamp: (new Date(timeStamp)).toLocaleDateString(), saleID: [], ticketNumber: [], total: "0" });
temp.saleID.push(object.saleID);
temp.ticketNumber.push(object.ticketNumber);
temp.calcTotal = (parseInt(temp.calcTotal) + parseInt(object.calcTotal)).toString();
temp.total = (parseInt(temp.total) + parseInt(object.total)).toString();
return r;
}, []);
console.log(result);
I have seen similar questions about duplicate objects.
my json output:
{ "coupons": [
{
"id": "376363",
"price": "14400",
"date": "2018-04-08 10:40:17",
"user_id": "16433",
"ip": "46.225.123.235",
"code": "5ac9b249a0cc5",
"succ": "1",
"admin_seen": "1",
"user_seen": "0",
"coupon_id": "20821",
"coupon_parent": "20821",
"coupon_code": "195_2484C1_9873(7531)",
"coupon_code_user": "7531",
"coupon_code_partner": "195_2484C1_9873",
"shop_id": "2484",
"payment_type": "7",
"merchent_type": "3",
"merchent_id": "0",
"cradit_start_date": "2018-03-24 05:05:05",
"cradit_end_date": "2018-04-22 05:05:05",
"expired": "0",
"pay_data": null,
"seri": "C",
"to_friend": "0",
"finance_id": "0",
"app": "web",
"expire_date": "0000-00-00 00:00:00",
"expire_app": "",
"buy_id": "5ac9b249970c2",
"coupon_property_id": "0",
"title": "title1",
"coupon_property_title": "",
"parent_title": ""
},
{
"id": "376362",
"price": "14400",
"date": "2018-04-08 10:40:17",
"user_id": "16433",
"ip": "46.225.123.235",
"code": "5ac9b24997103",
"succ": "1",
"admin_seen": "1",
"user_seen": "0",
"coupon_id": "20821",
"coupon_parent": "20821",
"coupon_code": "194_2484C1_9779(4478)",
"coupon_code_user": "4478",
"coupon_code_partner": "194_2484C1_9779",
"shop_id": "2484",
"payment_type": "7",
"merchent_type": "3",
"merchent_id": "0",
"cradit_start_date": "2018-03-24 05:05:05",
"cradit_end_date": "2018-04-22 05:05:05",
"expired": "0",
"pay_data": null,
"seri": "C",
"to_friend": "0",
"finance_id": "0",
"app": "web",
"expire_date": "0000-00-00 00:00:00",
"expire_app": "",
"buy_id": "5ac9b249970c2",
"coupon_property_id": "0",
"title": "title2",
"coupon_property_title": "",
"parent_title": ""
},
{
"id": "341459",
"price": "27000",
"date": "2017-03-07 10:42:47",
"user_id": "16433",
"ip": "46.225.76.21",
"code": "58be5d6fd7214",
"succ": "1",
"admin_seen": "1",
"user_seen": "0",
"coupon_id": "19457",
"coupon_parent": "19457",
"coupon_code": "7_1310B1_2389(3386)",
"coupon_code_user": "3386",
"coupon_code_partner": "7_1310B1_2389",
"shop_id": "1310",
"payment_type": "7",
"merchent_type": "3",
"merchent_id": "0",
"cradit_start_date": "2017-01-16 05:05:05",
"cradit_end_date": "2017-03-19 05:05:05",
"expired": "11",
"pay_data": null,
"seri": "B",
"to_friend": "0",
"finance_id": "0",
"app": "web",
"expire_date": "0000-00-00 00:00:00",
"expire_app": "",
"buy_id": "58be5d6fd71c6",
"coupon_property_id": "0",
"title": "title3",
"coupon_property_title": "",
"parent_title": ""
},
{
"id": "341456",
"price": "11250",
"date": "2017-03-07 10:34:54",
"user_id": "16433",
"ip": "46.225.76.21",
"code": "58be5b964bf1d",
"succ": "1",
"admin_seen": "1",
"user_seen": "0",
"coupon_id": "19724",
"coupon_parent": "19724",
"coupon_code": "16_2129A1_2178(4663)",
"coupon_code_user": "4663",
"coupon_code_partner": "16_2129A1_2178",
"shop_id": "2129",
"payment_type": "7",
"merchent_type": "3",
"merchent_id": "0",
"cradit_start_date": "2017-03-05 05:05:05",
"cradit_end_date": "2017-05-05 05:05:05",
"expired": "11",
"pay_data": null,
"seri": "A",
"to_friend": "0",
"finance_id": "0",
"app": "web",
"expire_date": "0000-00-00 00:00:00",
"expire_app": "",
"buy_id": "58be5b964b1a1",
"coupon_property_id": "0",
"title": "title4",
"coupon_property_title": "",
"parent_title": ""
}
]
}
each object has a buy_id field. buy_id shows that the objects are the same and I should print them inside one row of html table.
for example the first object and the second object are the same depending on buy_id I print them inside row. output:
---------
title1 - title2 //because the first object and second object have same `buy_id`
---------
title3
---------
title4
should I use two maps?
I should use this scenario inside return of render function.
I put a bet on reduce for that.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
the following snippets will group your objects by buy_id, then you can render them easily
const data = { "coupons": [
{
"id": "376363",
"price": "14400",
"date": "2018-04-08 10:40:17",
"user_id": "16433",
"ip": "46.225.123.235",
"code": "5ac9b249a0cc5",
"succ": "1",
"admin_seen": "1",
"user_seen": "0",
"coupon_id": "20821",
"coupon_parent": "20821",
"coupon_code": "195_2484C1_9873(7531)",
"coupon_code_user": "7531",
"coupon_code_partner": "195_2484C1_9873",
"shop_id": "2484",
"payment_type": "7",
"merchent_type": "3",
"merchent_id": "0",
"cradit_start_date": "2018-03-24 05:05:05",
"cradit_end_date": "2018-04-22 05:05:05",
"expired": "0",
"pay_data": null,
"seri": "C",
"to_friend": "0",
"finance_id": "0",
"app": "web",
"expire_date": "0000-00-00 00:00:00",
"expire_app": "",
"buy_id": "5ac9b249970c2",
"coupon_property_id": "0",
"title": "title1",
"coupon_property_title": "",
"parent_title": ""
},
{
"id": "376362",
"price": "14400",
"date": "2018-04-08 10:40:17",
"user_id": "16433",
"ip": "46.225.123.235",
"code": "5ac9b24997103",
"succ": "1",
"admin_seen": "1",
"user_seen": "0",
"coupon_id": "20821",
"coupon_parent": "20821",
"coupon_code": "194_2484C1_9779(4478)",
"coupon_code_user": "4478",
"coupon_code_partner": "194_2484C1_9779",
"shop_id": "2484",
"payment_type": "7",
"merchent_type": "3",
"merchent_id": "0",
"cradit_start_date": "2018-03-24 05:05:05",
"cradit_end_date": "2018-04-22 05:05:05",
"expired": "0",
"pay_data": null,
"seri": "C",
"to_friend": "0",
"finance_id": "0",
"app": "web",
"expire_date": "0000-00-00 00:00:00",
"expire_app": "",
"buy_id": "5ac9b249970c2",
"coupon_property_id": "0",
"title": "title2",
"coupon_property_title": "",
"parent_title": ""
},
{
"id": "341459",
"price": "27000",
"date": "2017-03-07 10:42:47",
"user_id": "16433",
"ip": "46.225.76.21",
"code": "58be5d6fd7214",
"succ": "1",
"admin_seen": "1",
"user_seen": "0",
"coupon_id": "19457",
"coupon_parent": "19457",
"coupon_code": "7_1310B1_2389(3386)",
"coupon_code_user": "3386",
"coupon_code_partner": "7_1310B1_2389",
"shop_id": "1310",
"payment_type": "7",
"merchent_type": "3",
"merchent_id": "0",
"cradit_start_date": "2017-01-16 05:05:05",
"cradit_end_date": "2017-03-19 05:05:05",
"expired": "11",
"pay_data": null,
"seri": "B",
"to_friend": "0",
"finance_id": "0",
"app": "web",
"expire_date": "0000-00-00 00:00:00",
"expire_app": "",
"buy_id": "58be5d6fd71c6",
"coupon_property_id": "0",
"title": "title3",
"coupon_property_title": "",
"parent_title": ""
},
{
"id": "341456",
"price": "11250",
"date": "2017-03-07 10:34:54",
"user_id": "16433",
"ip": "46.225.76.21",
"code": "58be5b964bf1d",
"succ": "1",
"admin_seen": "1",
"user_seen": "0",
"coupon_id": "19724",
"coupon_parent": "19724",
"coupon_code": "16_2129A1_2178(4663)",
"coupon_code_user": "4663",
"coupon_code_partner": "16_2129A1_2178",
"shop_id": "2129",
"payment_type": "7",
"merchent_type": "3",
"merchent_id": "0",
"cradit_start_date": "2017-03-05 05:05:05",
"cradit_end_date": "2017-05-05 05:05:05",
"expired": "11",
"pay_data": null,
"seri": "A",
"to_friend": "0",
"finance_id": "0",
"app": "web",
"expire_date": "0000-00-00 00:00:00",
"expire_app": "",
"buy_id": "58be5b964b1a1",
"coupon_property_id": "0",
"title": "title4",
"coupon_property_title": "",
"parent_title": ""
}
]
};
const reducedData = data.coupons.reduce((accumulator, value) => {
accumulator[value.buy_id] = accumulator[value.buy_id] || [];
accumulator[value.buy_id].push(value);
return accumulator;
}, {});
console.log(reducedData);
You have to use reduce :
data.coupons.reduce((acc,value)=>{
return acc[value.buy_id] === undefined
? {...acc,[value.buy_id]:value.title}
: {...acc,[value.buy_id]:`${acc[value.buy_id]} - ${value.title}`}
},{})
The result is :
{
"5ac9b249970c2": "title1 - title2",
"58be5d6fd71c6": "title3",
"58be5b964b1a1": "title4"
}
I'm trying render a column chart using json for xAxis categories and the series. Unfortunately no column chart been generated as shown in picture below.
Here is my JSON structure (already validate using JSON validator online)
{
"Graph": {
"Series": [
{
"name": "John",
"data": [
"51",
"84",
"61",
"45",
"51",
"0",
"0",
"53",
"83",
"50",
"41",
"45",
"0",
"0",
"0",
"40",
"52",
"60",
"48",
"0",
"0",
"60",
"0",
"67",
"58",
"0",
"0",
"0",
"0",
"0"
]
},
{
"name": "Doe",
"data": [
"1",
"0",
"0",
"1",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"1",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0"
]
}
],
"Categories": [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
"28",
"29",
"30"
]
}
}
Here is my javascript
var dchart = {
chart: {
type: 'column'
},
title: {
text: ''
},
xAxis: {
categories: []
},
yAxis: {
min: 0,
title: {
text: 'Applicants'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y}</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
groupPadding: 0.1,
pointPadding:0.1,
events: {
legendItemClick: function () {
return false;
}
}
},
series: {
dataLabels:{
enabled:true,
formatter: function() {
if(this.y>0){
return '<div style="text-align:center; font-family:Arial,Helvetica,sans-serif; font-weight:bold;">' + this.y + '</div>';
}
}
}
}
},
series: []
}
$.getJSON("<%=request.getContextPath()%>/c/MonthlyGraphAjax?method=ApplicantByDaily", function(data){
/*xAxis categories*/
for(var x=0;x<data.Graph.Categories.length;x++){
dchart.xAxis.categories.push(data.Graph.Categories[x])
}
/*series*/
for(var z=0;z<data.Graph.Series.length;z++){
dchart.series.push(data.Graph.Series[z])
}
$("#columnGraphContainer").highcharts(dchart)
})
Can anyone help me solved this problem? I think the below code causing the problem
/*series*/
for(var z=0;z<data.Graph.Series.length;z++){
dchart.series.push(data.Graph.Series[z])
}
Sorry for the inconvinience, here is the jsfiddle link for clear view of my problem. Highchart With No Column Bar
HighCharts was expecting numeric values for the data series, but received strings instead and didn't know how to display those on the graph. If you change your input from type String to type Number, it will fix your problem.
"Series": [
{
"name": "John",
"data": [
51,
84,
61,
...
]
}
]
I've forked your fiddle here with a working solution: http://jsfiddle.net/autoboxer/s8symkdz/.
Cheers,
autoboxer