如何在 angular 中的另一个对象中访问名为 属性 的动态?
How to access dynamic named property inside another object in angular?
在Angular8中我从一个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);
}
在Angular8中我从一个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);
}