将对象的数组组合到JavaScript中的一个对象
let myCurrentData = [
{
_id: "D01",
name: "Lunetts",
profession: "shop",
},
{
_id: "D02",
name: "Glasses",
profession: "keeper",
},
{
_id: "D03",
name: "Auros",
profession: "UiiSii",
},
];
上面是我的mycurrentdata,我想将此数组转换为最终对象如下
let myFinalData = {
D01: "Lunetts",
D02: "Glasses",
D03: "Auros"
}
我只想使用_id和名称的值
let myCurrentData = [
{
_id: "D01",
name: "Lunetts",
profession: "shop",
},
{
_id: "D02",
name: "Glasses",
profession: "keeper",
},
{
_id: "D03",
name: "Auros",
profession: "UiiSii",
},
];
Above is my myCurrentData, I want to convert this array into a final object like the following
let myFinalData = {
D01: "Lunetts",
D02: "Glasses",
D03: "Auros"
}
i just want to use the values of _id and name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
希望它有效!
Hope it works!
Use a simple loop to create a new object.
您可以使用降低并从中返回对象来实现此目的:
You can achieve this using reduce and returning an object from it:
您可以使用对象的优势。进入方法与foreach循环相结合以获取属性名称和值并将其添加到先前声明的myFinalData对象中。像这样的东西可能是您想要的。希望它有帮助,芽。
You could take advantages of the Object.entries method combined with a forEach loop to get the properties name and values and add it to your previously declare myFinalData Object. Something like this could be what you are looking for. Hope it helps, bud.