JS Merging object properties from an array of objects by matching ids

I have two arrays of objects. I want to look at the first one, find … Read more JS Merging object properties from an array of objects by matching ids

I have two arrays of objects. I want to look at the first one, find typeId then look in the second array for the match (states.typeId == stateTypes.id) then merge those properties into the first array’s found match object. If the properties have same key append "stateType" to the property name if not just bring it over. I think an example would best explain it.

Array of objects

"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"
        }
    ],

Wanted array

 "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"
        }
    ],

Source: JavaSript – Stack Overflow


Categories: NewsTags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *