【发布时间】:2021-08-24 22:45:55
【问题描述】:
我有两个对象数组。我想查看第一个,找到 typeId 然后在第二个数组中查找匹配项(states.typeId == stateTypes.id),然后将这些属性合并到第一个数组找到的匹配对象中。如果属性具有相同的键,则将“stateType”附加到属性名称(如果不只是将其带过来)。我认为举个例子最能说明问题。
对象数组
"states": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text"
},
{
"id": 3,
"typeId": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text"
}
],
"stateTypes": [
{
"id": 1,
"name": "PENDING",
"description": "Pending",
"label": "Pending",
"newIncomingKey": "Text"
},
{
"id": 2,
"name": "IN_PROGRESS",
"description": "In Progress",
"label": "In Progress",
"newIncomingKey": "Text"
},
{
"id": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"newIncomingKey": "Text"
}
],
想要的数组
"newArray": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 1,
"stageType-name": "PENDING",
"stageType-description": "Pending",
"stageType-label": "Pending"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 3,
"stageType-name": "COMPLETED",
"stageType-description": "Completed",
"stageType-label": "Completed"
},
{
"id": 3,
"typeId": 2,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 2,
"stageType-name": "IN_PROGRESS",
"stageType-description": "In Progress",
"stageType-label": "In Progress"
}
],
【问题讨论】:
-
欢迎来到 Stack Overflow! Stack Overflow 用于编程问题,而不是关于使用或配置 Unix 及其实用程序的问题。 Unix & Linux 或 Super User 将是解决此类问题的更好地方。
-
熟悉how to access and process nested objects, arrays or JSON 以及如何使用create objects 并使用
Object和Array的可用静态和实例方法,然后自己尝试一下。 Stack Overflow 不是免费的代码编写服务。
标签: javascript arrays object merge