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

95
Vistas
Return object from array of objects

I have the following array of objects:

const values = [
    {
      clientType: "Client Type 1",
      value: 130
    },
    {
      clientType: "Client Type 2",
      value: 10
    },
    {
      clientType: "Client Type 3",
      value: -80
    },
    {
      clientType: "Client Type 4",
      value: -52
    }
  ]

I want to "map" this array and get as a result the following oject:

results = {
  "Client Type 1": 130,
  "Client Type 2": 10,
  "Client Type 3": -80,
  "Client Type 4": -52,
}

Is there a way of doing this directly? (Using only one map function)

TIA

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

0

const values = [
    {
      clientType: "Client Type 1",
      value: 130
    },
    {
      clientType: "Client Type 2",
      value: 10
    },
    {
      clientType: "Client Type 3",
      value: -80
    },
    {
      clientType: "Client Type 4",
      value: -52
    }
  ]
  
const result = values.reduce((acc, {clientType, value}) => ({ ...acc, [clientType]: value}), {})

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is a fairly simple question/task so I will try to post a simple, easy to understand answer.

const values = [{
      clientType: "Client Type 1",
      value: 130
    },
    {
      clientType: "Client Type 2",
      value: 10
    },
    {
      clientType: "Client Type 3",
      value: -80
    },
    {
      clientType: "Client Type 4",
      value: -52
    }
  ],
  // loop through "values" object and construct and object the way the OP needs then return it.
  resultObj = values.reduce((a, c) => {
    // a: is the object that we are constructing, its default value is {} (empty object)
    // c: is the current object from the "values" array
    a[c.clientType] = c.value;
    return a;
  }, {});

// this line is not needed, it just prints the result to the console
console.log(resultObj);

Just a sidenote (but rather important), the only way to access an attribute on the resulted Object is to use brackets notation: resultObj['Client Type 1'] // prints: 130

Learn more about reduce method on MDN.

about 4 years ago · Juan Pablo Isaza Denunciar

0

This code seems to work:

const values = [
{
  clientType: "Client Type 1",
  value: 130
},
{
  clientType: "Client Type 2",
  value: 10
},
{
  clientType: "Client Type 3",
  value: -80
},
{
  clientType: "Client Type 4",
  value: -52
}
  ]

values.map(getFull);

function getFull(item) {
  return [item.clientType,item.value].join(" ");
}

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