【发布时间】:2022-01-25 07:12:52
【问题描述】:
我正在构建一个允许我比较两个对象的组件。它接受要比较的字段列表和需要忽略的字段列表字符串格式
这里是一个将被比较的对象的例子:
{
// (..... More elements above .....)
taskData: {
"uniqueId": "OrdenTrabajo48",
"id": 48,
"position": 1,
"name": "Dirección Obra Civil",
"description": "Dirección Obra Civil Afecta: Sorda, Roberto",
"startDate": "2021-10-16T11:00:00.000Z",
"endDate": "2022-06-01T11:00:00.000Z",
"duration": 227,
"progress": 73,
"hours": 0,
"realHours": 15,
"predecessor": null,
"child": [],
"resourceInfo": [
{
"uniqueId": "Persona_1MJ0VE9G0",
"id": "OrdenTrabajo48Persona_1MJ0VE9G0",
"name": "Sorda, Roberto",
"group": "Subgerencia de Planes y Proyectos - SUB_PLAN_PROY_SIT",
"unit": 4.1667,
"startDate": "2021-10-16T03:00:00.000+00:00",
"endDate": "2022-06-01T02:59:59.000+00:00",
"hours": 0,
"realHours": 15,
"avatar": "http://localhost:8091/images/llama.jpg"
}
],
"comments": null,
"etiquetas": [],
"baseLineStartDate": null,
"baseLineEndDate": null
}
// (..... More elements below .....)
}
(但澄清一下,它可以是任何对象。组件是抽象的,可以在任何地方使用)
组件不知道要比较的对象的结构,只知道对象和字符串格式的路径
我想删除数组的每个元素resourceInfo,属性avatar、icon、label和color不管数组的长度如何,但我不知道是否有这样做的语法。
我也想删除属性realHours
这是我尝试过的:
const ignoredFields = [
'taskData.resourceInfo[?].avatar', //<--- It's not working
'taskData.resourceInfo[].icon', //<--- Neither this
'taskData.resourceInfo.label', //<--- Or this
'taskData.resourceInfo[0].color', //<--- This hardcode is working, but I don't know the length in that scope
'taskData.realHours' // <-- No problems here
];
const currentComparableObject = _.omit(obj, ignoredFields);
const oldComparableObject = _.omit(prev, ignoredFields);
【问题讨论】:
-
我将添加另一个步骤并使用路径
taskData.resourceInfo并迭代该数组并在每个对象上省略数组字段['avatar','icon','label']等 -
对,我怎么能把它抽象化?比较两个对象的组件并不真正知道需要比较的对象的详细信息。它只知道一个对象和需要忽略的属性的名称
-
然后,如果您不知道结构,则需要递归跟踪路径并同时遍历两个对象。我现在有点困惑,因为你似乎知道你想要什么路径
-
我知道我想要比较和忽略的路径(父组件提供这些),但我不知道抽象组件中模型的结构
标签: javascript lodash