Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
Devolver objeto de una matriz de objetos

Tengo la siguiente matriz de objetos:

 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 } ]

Quiero "mapear" esta matriz y obtener como resultado el siguiente objeto:

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

¿Hay alguna manera de hacer esto directamente? (Usando solo una función de mapa)

AIT

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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 Report

0

Esta es una pregunta/tarea bastante simple, así que intentaré publicar una respuesta simple y fácil de entender.

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

Solo una nota al margen (pero bastante importante), la única forma de acceder a un atributo en el objeto resultante es usar la notación de corchetes: resultObj['Client Type 1'] // prints: 130

Obtén más información sobre el método de reduce en MDN.

about 4 years ago · Juan Pablo Isaza Report

0

Este código parece funcionar:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!