1

So I have this array:

 [{date: "5/7/2021", name: "bob"},
 {date: "5/6/2021", name: "bob"},
 {date: "5/6/2021", name: "bob"},
 {date: "5/6/2021", name: "bob"},]

How to make the array into:

[ {"5/6/2021" : 3}, // 3 because there are 3 data with 5/6/2021 date
  {"5/7/2021" : 1}] // 1 because there are 1 data with 5/7/2021 date

My Progress. updatedSales is the array name (Edit)

const temp = updatedSales.reduce((acc, { name, date }) => {
        acc[name] = acc[name] || { name, dates: new Set(), totalSubmit: 0 };
        acc[name].totalSubmit++;
        acc[name].dates.add(date);
        return acc;
    }, {});

    const result = Object.values(temp).map(({ name, dates, totalSubmit }) => ({ name, uniqueDate: dates.size, totalSubmit }));

 const updatedSales=[{date: "5/7/2021", name: "bob"},
 {date: "5/6/2021", name: "bob"},
 {date: "5/6/2021", name: "bob"},
 {date: "5/6/2021", name: "bob"},]

const temp = updatedSales.reduce((acc, { name, date }) => {
        acc[name] = acc[name] || { name, dates: new Set(), totalSubmit: 0 };
        acc[name].totalSubmit++;
        acc[name].dates.add(date);
        return acc;
    }, {});

    const result = Object.values(temp).map(({ name, dates, totalSubmit }) => ({ name, uniqueDate: dates.size, totalSubmit }));
    
console.log(result)

Expectation:

[ {"5/6/2021" : 3}, // 3 because there are 3 data with 5/6/2021 date
  {"5/7/2021" : 1}]

Right now:

[{name: "bob", uniqueDate: 3, totalSubmit: 36},
 {name: "steve", uniqueDate: 12, totalSubmit: 116}]

My desired output is the date to be different, not name. So its like "5/6/2021" instead of "bob".

Edward Tanoto
  • 133
  • 10
  • 1
    Please show some code of what you've tried – koralarts May 18 '21 at 17:20
  • So to be clear, updatedSales is the name of the array. This codes was served for another purposes, but this is my progress so far. (Edit: I'll put my progress in the edit) @koralarts – Edward Tanoto May 18 '21 at 17:25
  • 1
    It looks like you're basically there. What is your actual question? Please read [how to ask](https://stackoverflow.com/help/how-to-ask) before asking future questions. – Andy Ray May 18 '21 at 17:29
  • @AndyRay Not yet, I kind of need help on the reduce part. – Edward Tanoto May 18 '21 at 17:29
  • 1
    What do "need help" and "how to manipulate" mean? What part are you having trouble with specifically? What output are you seeing that doesn't match your expectations and which part of that are you having trouble with? – Andy Ray May 18 '21 at 17:30
  • 1
    You are currently grouping by `name`. Try replacing `name` in your `.reduce()`-callback function by `date`. Then you should already get closer to what you want. – Carsten Massmann May 18 '21 at 17:33
  • @CarstenMassmann Thank you so much for your help, I've got it. – Edward Tanoto May 18 '21 at 17:38

1 Answers1

0

With just a few touches to your script you will get this:

const updatedSales=[{date: "5/7/2021", name: "bob"},
 {date: "5/6/2021", name: "bob"},
 {date: "5/6/2021", name: "bob"},
 {date: "5/6/2021", name: "bob"},]

const temp = updatedSales.reduce((acc, { name, date }) => {
        acc[date] = acc[date] || { date, dates: new Set(), totalSubmit: 0 };
        acc[date].totalSubmit++;
        acc[date].dates.add(date);
        return acc;
    }, {});

    const result = Object.values(temp).map(({ date, dates, totalSubmit }) => ( {[date]: totalSubmit }) );
    
console.log(result)

Or, if you really want to, you can reduce it down to a "one-liner":

const result = Object.entries(
    updatedSales.reduce((a,{date}) => {
      a[date] = (a[date] || 0) + 1;
      return a;  
    }, {})   ).map(([k,v])=> ( {[k]:v }) );

The new Set() is not really required here.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • Hi, thank you so much for your help, do you mind checking through this too: https://stackoverflow.com/questions/67592355/how-to-show-filtered-data-in-javascript-callback-function-react-calendar-month-v? I really appreciate it. Have a good day! – Edward Tanoto May 18 '21 at 19:34