更新时间:2023-01-16 15:56:17
您应该意识到您的领域可能非常复杂,但在您提到的情况下,可以通过以下方式解决:
You should be aware that your field can be very complex but in the case you mentioned, can be resolved with this:
resolveField(data: any, field: string): any {
if (data && field) {
let fields: string[] = field.split('.');
let value = data;
for(let i = 0, len = fields.length; i < len; ++i) {
if (value == null) {
return null;
} else {
const pos: number = fields[i].match(/\d+/g)?.[0];
if(pos != null) {
const property = fields[i].match(/^\w+/g);
value = value[property][pos];
} else {
value = value[fields[i]];
}
}
}
return value;
} else {
return null;
}
}
您可以将 resolveField 函数用作逻辑的一部分.您可以根据需要或需要对其进行修改,例如:这里仅考虑将字母作为属性名称的一部分.
You can use the resolveField function as part of your logic. You can modify it, as you want or required, for example: here is considering only letters as part of the property names.
这是解决方案 用于您的代码.