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

112
Vistas
Transform an tab of key values into an array of key values

I would like to transform an tab array of key values into an array of key values

     rates.map(rate => {
       return {
         [rate]: account.vent.filter(v => v.rate === rate)[0]
           ?.vat,
       }
     })


(5) [{…}, {…}, {…}, {…}, {…}]
0: {10: 500}
1: {20: 630}
2: {5: undefined}
3: {19.6: undefined}
4: {1: undefined}

I have this in my console I would like something more like:

{
  10: 500
  20: 630
  5: undefined
  19.6: undefined
  1: undefined
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It looks like reduce is a more appropriate method to use for your case, since you want to create an object out of an array.

something like

 rates.reduce((ratesObj, rate) => {
   ratesObj[rate] = account.vent.find(v => v.rate === rate)?.vat;
   return ratesObj;
 }, {})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Add this and your problems will be solved

.reduce(function(p,c){
    return {...p,...c};
},{})

Your code will look like this after adding the above code

rates.map(rate => {
       return {
         [rate]: account.vent.filter(v => v.rate === rate)[0]
           ?.vat,
       }
     })
.reduce(function(p,c){
    return {...p,...c};
},{})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this

array.reduce((acc, item)=>{
    return {...acc, ...item}
},{})

Reduce Method:- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

A good video on reduce method:- https://www.youtube.com/watch?v=IfieE2G3bfg

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