1

I have an Array full of transactions and I want to divide it by day. It will be an array of date that is and array of transations. It may be a little messy but I want to return this structure.

What I tried to do returns me the structure I want, but I don't know how to merge duplicated key values.

This is the array

const transactions = [
  {
    name: "Salário",
    receiveDate: "2020-05-12T00:00:00.000Z",
    value: "1000",
  },
  {
    name: "Pagamento ",
    receiveDate: "2020-05-12T00:00:00.000Z",
    value: "2350",
  },
  {
    name: "Passagem no VEM",
    paidDate: "2020-05-02T00:00:00.000Z",
    value: "130",
  },
  {
    name: "Almoço",
    paidDate: "2020-05-08T00:00:00.000Z",
    value: "50",
  },
];

This is what I already tried by now

const days = [];
const finalArray = [];

for (let i = 0; i < transactions.length; i++) {
  transactions[i].day = transactions[i].receiveDate || transactions[i].paidDate;
  days.push(transactions[i].day);
}

const datesToMatch = [...new Set(days)].map((date) => {
  return { [date]: null };
});

transactions.map((transaction) => {
  datesToMatch.map((dayObject) => {
    const day = Object.keys(dayObject).toString();
    if (day === transaction.day) {
      finalArray.push({ [day]: [transaction] });
    }
  });
});

The output

[ { '2020-05-12T00:00:00.000Z': [ [Object] ] },
  { '2020-05-12T00:00:00.000Z': [ [Object] ] },
  { '2020-05-02T00:00:00.000Z': [ [Object] ] },
  { '2020-05-08T00:00:00.000Z': [ [Object] ] } ]

Expected output

[ { '2020-05-12T00:00:00.000Z': [ [Object, Object] ] },
  { '2020-05-02T00:00:00.000Z': [ [Object] ] },
  { '2020-05-08T00:00:00.000Z': [ [Object] ] } ]

Thanks!

  • Do you want to turn date values into keys and each key (date) has an array that contains names & values ? – Menai Ala Eddine - Aladdin May 02 '20 at 01:38
  • It seems like you want to [group the objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects), but in a somewhat strange way. – 3limin4t0r May 02 '20 at 01:43

3 Answers3

2

Explanation:

  • dates : extract dates from both fields
  • uniqueDates : build a Set and convert it into an array so it only has uniqueDates
  • dateToTransactions : map every unique date to an object with one key (itself) and filter every transaction that is equal to it.

const transactions = [{
    name: "Salário",
    receiveDate: "2020-05-12T00:00:00.000Z",
    value: "1000",
  },
  {
    name: "Pagamento ",
    receiveDate: "2020-05-12T00:00:00.000Z",
    value: "2350",
  },
  {
    name: "Passagem no VEM",
    paidDate: "2020-05-02T00:00:00.000Z",
    value: "130",
  },
  {
    name: "Almoço",
    paidDate: "2020-05-08T00:00:00.000Z",
    value: "50",
  },
];

const dates = transactions.map(x => {
  const received = x.receiveDate || [];
  const paid = x.paidDate || [];
  return received + paid;
});

const uniqueDates = [...new Set(dates)];

const dateToTransactions =
  uniqueDates.map(
    date => {
      sameDate = transactions.filter(x => x.receiveDate === date || x.paidDate == date);
      return {[date]: sameDate}; 
    });

console.log(dateToTransactions);
1

I would do something like this:

const days = [];

for (let i = 0; i < transactions.length; i++) {
  transactions[i].day = transactions[i].receiveDate || transactions[i].paidDate;
  days.push(transactions[i].day);
}

const result = new Map();
days.forEach((day) => {
  result.set(day, [])
});

transactions.forEach((transaction) => {
  let r = result.get(transaction.day);
  r.push(transaction);
  result.set(transaction.day, r);
});

Then, in the result map you have a list of the transactions that were made for each day.

Alejandro De Cicco
  • 1,216
  • 3
  • 17
1

This will give the result you expect

const days = {};

const finalArray = transactions.forEach((transaction) => {
          let date = (transaction.receiveDate || transaction.paidDate)
          if (!days[date]) { days[date] = [transaction]}
          else {days[date].push(transaction)}
          });
console.log(days);
pierrecode
  • 192
  • 2
  • 15