javascript - 如何获取对象数组中键的值?
问题描述
我有一个结构如下的对象数组:
import icon1 from '!!raw-loader!../../../icon1.svg';
import icon2 from '!!raw-loader!../../../icon2.svg';
import icon3 from '!!raw-loader!../../../icon3.svg';
import icon4 from '!!raw-loader!../../../icon4.svg';
import icon5 from '!!raw-loader!../../../icon5.svg';
export const CustomIcons = [
{
home: 'icon1',
homeIcon: (icon)
},
{
series: 'icon2',
seriesIcon: (icon2)
},
{
explorer: 'icon3',
explorerIcon: (icon3)
},
...
];
在我的模块的构造函数中,我想使用如下值:
export class IconRegistryModule {
constructor(iconRegistry: MatIconRegistry, sanitizer: DomSanitizer) {
CustomIcons.forEach(icon => {
iconRegistry.addSvgIconLiteral(Object.keys(icon)[0], sanitizer.bypassSecurityTrustHtml(Object.keys(icon)[1]));
});
}
}
因此,对于 forEarch 的第一次运行,addSvgIconLiteral 方法应该如下所示:
iconRegistry.addSvgIconLiteral('the value of the key home', sanitizer.bypassSecurityTrustHtml('the value of the key homeIcon');
解决方案
使用它来获取数组中的对象键。
CustomIcons.map((data) => {
console.log(Object.keys(data));
});
推荐阅读
- python - Python 的 re.findall 似乎在不在函数中时工作,但在函数中时失败
- c++ - 带有结构和数组的 GLSL/C++ std140 布局
- python - 满足条件时不执行第三个 ELIF 语句。在我重新运行代码后执行。Python
- javascript - 我在哪里运行 Jest 中测试设置所需的异步代码?
- ios - 如何从数组中获取 NSManaged Object ID 并将其删除?
- python - 我应该如何自动填充一个字段并在 django 中使其只读?
- sql - SQL Server 中的两个问题
- reactjs - 如何使用reactjs将评论计数分配给帖子
- html - html表单的移动视图响应
- sql - postgres 删除总计数较低的行