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