JSFiddle - React, Tailwind, and code Playground
by Avi Algaly
JavaScript
const data = [{
"CONSIGNEE": "1100",
"ORDERPRIORITY": 1,
"STATUSCODE": "UK",
"COUNTS": 2
}, {
"CONSIGNEE": "1100",
"ORDERPRIORITY": 1,
"STATUSCODE": "VS",
"COUNTS": 227
}, {
"CONSIGNEE": "2700",
"ORDERPRIORITY": 1,
"STATUSCODE": "UK",
"COUNTS": 2
}, {
"CONSIGNEE": "2700",
"ORDERPRIORITY": 1,
"STATUSCODE": "VS",
"COUNTS": 459
}, {
"CONSIGNEE": "3000",
"ORDERPRIORITY": 1,
"STATUSCODE": "UK",
"COUNTS": 12
}, {
"CONSIGNEE": "3000",
"ORDERPRIORITY": 1,
"STATUSCODE": "VS",
"COUNTS": 190
}, {
"CONSIGNEE": "4200",
"ORDERPRIORITY": 1,
"STATUSCODE": "NOT-VS",
"COUNTS": 4
}, {
"CONSIGNEE": "4200",
"ORDERPRIORITY": 1,
"STATUSCODE": "UK",
"COUNTS": 3
}, {
"CONSIGNEE": "4200",
"ORDERPRIORITY": 1,
"STATUSCODE": "VS",
"COUNTS": 435
}, {
"CONSIGNEE": "42CS",
"ORDERPRIORITY": 1,
"STATUSCODE": "NOT-VS",
"COUNTS": 1
}, {
"CONSIGNEE": "42CS",
"ORDERPRIORITY": 1,
"STATUSCODE": "VS",
"COUNTS": 22
}];
newCnames = [];
let cNames = data.map((itm) => {
if (newCnames.indexOf(itm.CONSIGNEE)<0)
if (itm.STATUSCODE === 'NOT-VS') {
vals={novs:itm.COUNTS,vs:0,uk:0}
}
else if (itm.STATUSCODE === 'VS') {
vals={novs:0,vs:itm.COUNTS,uk:0}
} else {
vals={novs:0,vs:0,uk:itm.COUNTS}
}
newCnames.push({CONSIGNEE:itm.CONSIGNEE,vals});
});
console.log(newCnames)
//run on each and sum consignee
let rawData=[]
function calc() {
return Object.values(newCnames).reduce((acc,consignee) =>{
let vals;
if (consignee.STATUSCODE === 'NOT-VS') {
vals={novs:consignee.COUNTS,vs:0}
}
else if (consignee.STATUSCODE === 'VS') {
vals={novs:0,vs:consignee.COUNTS}
} else {
vals={novs:0,vs:0}
}
const row= {consignee:consignee.CONSIGNEE,vs:consignee.COUNTS,novs:consignee.COUNTS,sum:0};
acc.push(row)
/* rawData[consignee].forEach((itm) => {
row.values.push(itm.Counts);
}); */
return acc;
},[])
}
//console.log(calc());