简体   繁体   English

从对象数组返回 object

[英]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) (仅使用一个 map 功能)

TIA 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只是一个旁注(但相当重要),访问结果 Object 上的属性的唯一方法是使用括号表示法: resultObj['Client Type 1'] // prints: 130

Learn more about reduce method on MDN.在 MDN 上了解有关reduce方法的更多信息。

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


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从多维 object 返回对象数组? - How to return array of objects from multidimensional object? 对对象数组进行排序,并从对象返回特定值的数组 - Sort array of objects and return array of specific values from the object 使用属性值从对象数组中查找并返回 object - Find and Return an object from an array of array of objects using a property value 遍历对象,其中值是对象数组,并从对象数组的每个对象返回一个值 - Iterating through object Where Value is Array of Objects and return one value from each Objects of Array of Objects 从对象数组中返回键/值对最高的对象 - Return object with highest key/value pairs from an array of objects 从对象数组中的对象属性返回最大数字 - Return highest number from object property inside an Array of Objects 是否有更高阶函数从javascript中的对象数组返回一个对象? - Are there any higher order function to return an object from an array of objects in javascript? 从包含多个对象的数组中返回编号最高的对象 - Return object with highest number from array containing multiple objects 从由许多 object 属性分组的另一个对象返回数组 - return array of objects from another grouped by many object properties 在对象数组中搜索并返回JS中最后一个对应的Object的值 - Search in an array of objects and return a value from the last corresponding Object in JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM