angular - 如何以角度访问另一个对象内的动态命名属性?
问题描述
在 Angular 8 中,我从 JSON 创建了一个对象,有一个动态属性,在运行前我不知道它的名称。在另一个对象中,我需要调用该动态属性的值,但因为这发生在运行时,它不存在,因此它不会编译。
有没有办法做到这一点?
let i = 0;
this.columns = [
{ name: 'total' },
];
do {
if (certainCondition) {
this.column.push({ name: res.data[i].text })
}
this.rows.push({
total: someNumber,
res.data[i].text: someValue
});
i++;
} while (res.data[i] != null)
解决方案
您需要定义它的类型,或者简单地使用它any
来加速刺痛。用作res.data[i].text
键使用方括号[res.data[i].text]: value
。
(res: any) => {
let i = 0;
this.columns = [
{ name: 'total' },
];
do {
if (certainCondition) {
this.column.push({ name: res.data[i].text });
}
this.rows.push({
total: someNumber,
[res.data[i].text]: someValue
});
i++;
} while (res.data[i] != null);
}
推荐阅读
- c# - C# SendKeys.SendWait("^c") 不会在另一个应用程序中复制选定的文本
- firebase - Flutter/Firebase/Firestore - 注册不将数据推送到数据库
- ruby - 如何使用 gemspec add_runtime_dependency 和 `bundle install`
- reactjs - useCookie 钩子在构建时中断
- amazon-web-services - 如何通过 OIDC 和 EKS 使用 cdk 跨账户访问
- javascript - 如何在 NodeJS 中对具有相同属性值的对象进行分组?
- mongodb - mongodb数据库工具命令不会生效
- python - 从 GCS 加载 Tensorflow TPU 中的 tfdev.hub 模型
- python - 如何使用 loc 函数为每个索引赋予单独的值?
- java - 无法使用带有 jar 文件的 javac 类路径进行编译