Home > OS >  Reduce javascript map change key dynamically
Reduce javascript map change key dynamically

Time:05-13

I would like to have the array in output using the input array in the reduce function. But I can't manage to dynamically add a key like this "${keyOrigin}": cur.value })

Thanks for your help ! <3

const input = [{
    date: '10/03/40922',
    origin: 'Angleterre',
    value: 39
  },
  {
    date: '10/03/40922',
    origin: 'Espagne',
    value: 60
  },
  {
    date: '10/03/40922',
    origin: 'Argentine',
    value: 54
  },
  {
    date: '10/03/40922',
    origin: 'Italie',
    value: 45
  },
  {
    date: '10/03/40922',
    origin: 'Hollande',
    value: 40
  },
  {
    date: '10/03/40922',
    origin: 'Russie',
    value: 110
  },
  {
    date: '10/03/40922',
    origin: 'Suisse',
    value: 87
  },
  {
    date: '10/03/40922',
    origin: 'France',
    value: 143
  },
  {
    date: '11/03/40922',
    origin: 'Angleterre',
    value: 39
  },
  {
    date: '11/03/40922',
    origin: 'Espagne',
    value: 60
  },
  {
    date: '11/03/40922',
    origin: 'Argentine',
    value: 54
  },
  {
    date: '11/03/40922',
    origin: 'Italie',
    value: 45
  },
  {
    date: '11/03/40922',
    origin: 'Hollande',
    value: 40
  },
  {
    date: '11/03/40922',
    origin: 'Russie',
    value: 110
  },
  {
    date: '11/03/40922',
    origin: 'Suisse',
    value: 87
  },
  {
    date: '11/03/40922',
    origin: 'France',
    value: 143
  }
];

const output = [{
    date: "10/03/40922",
    Angleterre: 39,
    Espagne: 60,
    Argentine: 54,
    Italie: 45,
    Hollande: 40,
    Russie: 110,
    Suisse: 97,
    France: 143
  },
  {
    date: "11/03/40922",
    Angleterre: 39,
    Espagne: 60,
    Argentine: 54,
    Italie: 45,
    Hollande: 40,
    Russie: 110,
    Suisse: 97,
    France: 143
  },
];

const reduceByDate = (array) => {
  const result = Array.from(
    array
    .reduce((acc, cur) => {
      const key = JSON.stringify(cur.date);
      const current = acc.get(key) || {
        date: cur.date
      };
      const keyOrigin = JSON.stringify(cur.origin);

      return acc.set(key, { ...current,
        "`${keyOrigin}`": cur.value
      });
    }, new Map())
    .values()
  );
  return result;
};

console.log(reduceByDate(input))

CodePudding user response:

You meant to do use a computed property name

{ ...current, [cur.origin]: cur.value })

no need to stringify either - simplified - I am sure I could even get rid of the Map if I spent a little more time:

const reduceByDate = array => [...array
    .reduce((acc, cur) => {
      const date = cur.date;
      const current = acc.get(date) || { date };
      return acc.set(date, { ...current, [cur.origin]: cur.value });
    }, new Map())
  .values()];

const reduceByDate = array => [...array
    .reduce((acc, cur) => {
      const date = cur.date;
      const current = acc.get(date) || { date };
      return acc.set(date, { ...current, [cur.origin]: cur.value });
    }, new Map())
  .values()];

console.log(reduceByDate(input))
<script>
const input = [
  { date: '10/03/40922', origin: 'Angleterre', value: 39 },
  { date: '10/03/40922', origin: 'Espagne', value: 60 },
  { date: '10/03/40922', origin: 'Argentine', value: 54 },
  { date: '10/03/40922', origin: 'Italie', value: 45 },
  { date: '10/03/40922', origin: 'Hollande', value: 40 },
  { date: '10/03/40922', origin: 'Russie', value: 110 },
  { date: '10/03/40922', origin: 'Suisse', value: 87 },
  { date: '10/03/40922', origin: 'France', value: 143 },
  { date: '11/03/40922', origin: 'Angleterre', value: 39 },
  { date: '11/03/40922', origin: 'Espagne', value: 60 },
  { date: '11/03/40922', origin: 'Argentine', value: 54 },
  { date: '11/03/40922', origin: 'Italie', value: 45 },
  { date: '11/03/40922', origin: 'Hollande', value: 40 },
  { date: '11/03/40922', origin: 'Russie', value: 110 },
  { date: '11/03/40922', origin: 'Suisse', value: 87 },
  { date: '11/03/40922', origin: 'France', value: 143 }
];</script>

  • Related