Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
Reduce javascript map change key dynamically

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda