[英]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.