3

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

4 Answers 4

2

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);

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! The alternative map solution worked placing it inside of the reduce method helps return the unique count to its respective key in my original code.
1
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);

Comments

1

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
  }
]

Comments

1

And one more solution based on reduce() and Map.

With this solution, you can get the number of unique ids and also a breakdown of the number of orders with the same unique id.

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 map = orders.reduce((map, item) => {
    if(map.has(item.livingId)) {
        map.set(item.livingId, map.get(item.livingId) + 1);
    } else {
        map.set(item.livingId, 1);
    }
    return map;
}, new Map());

console.log("Unique Ids:", map.size);

const array = Array.from(map);

console.log(array);

The solution using Map can be enhanced to keep a running total of cash and tax (ignoring the fact that we should not be using floating points for storing money values).

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 map = orders.reduce((map, item) => {
    if(map.has(item.livingId)) {
        const {cash, tax} = map.get(item.livingId);
        map.set(item.livingId, {cash: cash + item.cash, tax: tax + item.tax });
    } else {
        map.set(item.livingId, {cash: item.cash, tax: item.tax });
    }
    return map;
}, new Map());

console.log("Unique Ids:", map.size);

const array = Array.from(map);

console.log(array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.