【问题标题】:JS Merging object properties from an array of objects by matching idsJS通过匹配id从对象数组中合并对象属性
【发布时间】: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"
        }
    ],

【问题讨论】:

标签: javascript arrays object merge


【解决方案1】:

类似这样的:

const state = [
        {
            "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"
        }
];
const 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"
        }
];

const temp = {};

for (const obj1 of state) {
  temp[obj1.id] = { ...obj1 };
}

for (const obj2 of stateTypes) {
  const destination = temp[obj2.id];

  for (const [key, value] of Object.entries(obj2)) {
    if (destination[key]) destination[`stateTypes-${key}`] = value;
    else destination[key] = value;
  }
}

const newArray = Object.values(temp);

console.log(newArray);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2018-02-08
    • 2021-12-28
    • 2019-02-20
    相关资源
    最近更新 更多