Hi I have been practicing challenges on reduce method to really understand it but this subject has confused me.
const orders = [
{ livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true},
{ livingId: 910, cash: 100, tax: 0.08, isDisputed: true },
{ livingId: 912, cash: 4200.11, tax: 0.06 },
{ livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false },
{ livingId: 910, cash: 0.00, tax: 0.08, isShipped: true },
{ livingId: 996, cash: 10, tax: 0.06, isDisputed: true },
];
const result = orders.reduce(
(arr, current) => {
const numOrders = arr.numOrders + 1;
const uniqueId = [...new Set([current.livingId])] //returns [996]
console.log("arr.uniqueId= "+ arr.uniqueId)
console.log("current.livingId= "+ current.livingId)
console.log("current.livingId + arr.uniqueId= "+ uniqueId)
return {
numOrders,
uniqueId,
};
},
{
numOrders: 0,
uniqueId: 0,
},
);
console.log(result)
How do I return the total count of UNIQUE id values from livingId? Together with my current method of getting total orders.
Expected result: NumOrders: 6 livingId: 3
I have tried console logging multiple methods like filter, includes and found the cleanest is the new Set method but it only returns the last iterable array from livingId returning [996]. Returning .length will definitely be 1 instead not 3. I also tried this but it returns back all the values instead.
const uniqueId = [...new Set([arr.uniqueId, current.livingId])] // 0,996,910,912,996,910,996
If you want to do this with reduce:
const orders=[{livingId:996,cash:30500.35,tax:.06,isDisputed:!0},{livingId:910,cash:100,tax:.08,isDisputed:!0},{livingId:912,cash:4200.11,tax:.06},{livingId:996,cash:99.12,tax:.06,isDisputed:!1},{livingId:910,cash:0,tax:.08,isShipped:!0},{livingId:996,cash:10,tax:.06,isDisputed:!0}];
const ids = orders.reduce((set, c) => {
set.add(c.livingId);
return set;
}, new Set());
console.log(ids.size);
Alternatively: map over the array to get an array of ids, add that array to a set, and get its size.
const orders = [
{ livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true},
{ livingId: 910, cash: 100, tax: 0.08, isDisputed: true },
{ livingId: 912, cash: 4200.11, tax: 0.06 },
{ livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false },
{ livingId: 910, cash: 0.00, tax: 0.08, isShipped: true },
{ livingId: 996, cash: 10, tax: 0.06, isDisputed: true }
];
const idsLen = new Set(orders.map(order => order.livingId)).size;
console.log(idsLen);
const orders = [
{ livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true},
{ livingId: 910, cash: 100, tax: 0.08, isDisputed: true },
{ livingId: 912, cash: 4200.11, tax: 0.06 },
{ livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false },
{ livingId: 910, cash: 0.00, tax: 0.08, isShipped: true },
{ livingId: 996, cash: 10, tax: 0.06, isDisputed: true },
];
const uniqueIds = orders.reduce((acc, val) => {
if(!acc.includes(val.livingId)) {
acc.push(val.livingId);
}
return acc
}, []);
const count = uniqueIds.length;
console.log(uniqueIds, count);
Another solution for you
const orders = [
{ livingId: 996, cash: 30500.35, tax: 0.06, isDisputed: true},
{ livingId: 910, cash: 100, tax: 0.08, isDisputed: true },
{ livingId: 912, cash: 4200.11, tax: 0.06 },
{ livingId: 996, cash: 99.12, tax: 0.06, isDisputed: false },
{ livingId: 910, cash: 0.00, tax: 0.08, isShipped: true },
{ livingId: 996, cash: 10, tax: 0.06, isDisputed: true },
];
const result = orders.reduce((acc,val) => {
let uniqueId = val.livingId
let obj = acc.find(a => a.uniqueId == val.livingId)
if(!!obj){
obj.numOrders++
}else{
acc.push({numOrders: 1, uniqueId: uniqueId})
}
return acc
},[]);
console.log(result)
Test Result
[
{
"numOrders": 3,
"uniqueId": 996
},
{
"numOrders": 2,
"uniqueId": 910
},
{
"numOrders": 1,
"uniqueId": 912
}
]