[英]How do we create object literals from arrays of object in javascript?
How do we create object literals from arrays of object in javascript?我们如何从 javascript 中的 object 的 arrays 创建 object 文字? , so that I can access name using a value or a number like for example I have literal object which is
,这样我就可以使用值或数字访问名称,例如我有文字 object 这是
#literal sample object #文字样本 object
SAMPLE_LITERALS {
'1': 'Sample1',
'2': 'Sample2',
'3': 'Sample3',
'4': 'Sample4'
}
then I can access a value for example SAMPLE_LITERALS[4]
and the result would be Sample4.然后我可以访问一个值,例如
SAMPLE_LITERALS[4]
,结果将是 Sample4。
I wanted to implement like that on my array of object sample below.我想在下面的 object 示例阵列上实现这样的功能。 Help would be appricated.
将提供帮助。 Thank you.
谢谢你。
I wanted to access the name.我想访问该名称。
#Code Query #代码查询
const team = await TeamsModel.findAll({
where: {
deletedAt: { [Op.eq]: null },
},
});
#result #结果
[
{
"id": 1,
"name": "Admin",
"createdAt": "2019-12-10T07:01:16.000Z",
"updatedAt": "2019-12-10T07:01:16.000Z",
"deletedAt": null,
"createdById": null,
"updatedById": null,
"createdBy": null,
"updatedBy": null
},
{
"id": 2,
"name": "Customer Support",
"createdAt": "2019-12-10T07:01:16.000Z",
"updatedAt": "2019-12-10T07:01:16.000Z",
"deletedAt": null,
"createdById": null,
"updatedById": null,
"createdBy": null,
"updatedBy": null
},
]
Assuming you want to index on id
, this is a pretty simple way to do that:假设您想在
id
上建立索引,这是一种非常简单的方法:
const teamsAsObject = {};
teams.forEach(team => {
teamsAsObject[team.id] = team;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.