简体   繁体   中英

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

 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)

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(" "); }

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.

I hope you will try this code I hope it works for you


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 results = {};
for (let i = 0; i < values.length; i++) {
  results[values[i].clientType] = values[i].value;
}

console.log("values", values);
// 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 }
//   ]

console.log("results", results);
//   results {
//     'Client Type 1': 130,
//     'Client Type 2': 10,
//     'Client Type 3': -80,
//     'Client Type 4': -52
//   }


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM