[英]Reducing an array of objects to key value map
I have an array of objects.我有一个对象数组。 I need to make an object so that I can fetch the name based on an id.
我需要制作一个 object 以便我可以根据 id 获取名称。
const drinks = [
{ _id: "5fe40ad4d2e6e6de85c46a6c", name: "Americano", __v: 0 },
{ _id: "5fe40ad4d2e6e6de85c46a6d", name: "Latte", __v: 0 },
{ _id: "5fe40ad4d2e6e6de85c46a6e", name: "Flat White", __v: 0 }
];
I tried this which almost works:我试过这个几乎可以工作:
console.log(drinks.reduce(
(accumulator, currentValue) =>
Object.assign(accumulator, accumulator[currentValue._id] = currentValue.name), {}));
However it outputs:但是它输出:
Object {
0: "F", 1: "l", 2: "a", 3: "t", 4: " ", 5: "W", 6: "h", 7: "i", 8: "t", 9: "e",
5fe40ad4d2e6e6de85c46a6c: "Americano",
5fe40ad4d2e6e6de85c46a6d: "Latte",
5fe40ad4d2e6e6de85c46a6e: "Flat White"
}
I need:我需要:
Object {
5fe40ad4d2e6e6de85c46a6c: "Americano",
5fe40ad4d2e6e6de85c46a6d: "Latte",
5fe40ad4d2e6e6de85c46a6e: "Flat White"
}
Why is my function also breaking down one of the values into characters?为什么我的 function 也将其中一个值分解为字符?
const drinks = [{ _id: "5fe40ad4d2e6e6de85c46a6c", name: "Americano", __v: 0 }, { _id: "5fe40ad4d2e6e6de85c46a6d", name: "Latte", __v: 0 }, { _id: "5fe40ad4d2e6e6de85c46a6e", name: "Flat White", __v: 0 }]; const result=drinks.reduce((acc,curr)=>{ acc[curr._id]=curr.name; return acc; },{}) console.log(result)
You can map your array into an array of entries (key / value pairs) which you can then run through Object.fromEntries()
to create the final object您可以将您的数组 map 放入一个条目数组(键/值对)中,然后您可以通过
Object.fromEntries()
运行以创建最终的 object
const drinks = [ { _id: "5fe40ad4d2e6e6de85c46a6c", name: "Americano", __v: 0 }, { _id: "5fe40ad4d2e6e6de85c46a6d", name: "Latte", __v: 0 }, { _id: "5fe40ad4d2e6e6de85c46a6e", name: "Flat White", __v: 0 } ] const result = Object.fromEntries(drinks.map(({ _id, name }) => [ _id, name ])) console.log(result)
The longer version would be something like this较长的版本将是这样的
const result = drinks.reduce((obj, { _id, name }) => ({
...obj, // spread syntax is like `Object.assign()`
[ _id ]: name // use `_id` as a dynamic object key with value `name`
}), {})
The issue with your current code is that the result of...您当前代码的问题是......
accumulator[currentValue._id] = currentValue.name
is the value assigned (ie currentValue.name
).是分配的值(即
currentValue.name
)。 When used as an argument in Object.assign()
, it is broken down as object entries, thus resulting in what you're seeing with当用作
Object.assign()
中的参数时,它被分解为 object 条目,从而产生您所看到的
{
0: "F",
1: "l",
2: "a",
3: "t",
4: " ",
5: "W",
6: "h",
7: "i",
8: "t",
9: "e"
}
Just pass the object { [currentValue._id]: currentValue.name }
to assign
instead of assignment accumulator[currentValue._id] = currentValue.name
只需通过 object
{ [currentValue._id]: currentValue.name }
来assign
而不是赋值accumulator[currentValue._id] = currentValue.name
const drinks = [ { _id: "5fe40ad4d2e6e6de85c46a6c", name: "Americano", __v: 0 }, { _id: "5fe40ad4d2e6e6de85c46a6d", name: "Latte", __v: 0 }, { _id: "5fe40ad4d2e6e6de85c46a6e", name: "Flat White", __v: 0 } ]; console.log(drinks.reduce( (accumulator, currentValue) => Object.assign(accumulator, { [currentValue._id]: currentValue.name }), {}));
This will work这将起作用
drinks.map(({ _id, name }) => ({
[_id]: name
}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.