-2

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"
        }
    ],
  • Welcome to Stack Overflow! [so] is for programming questions, not questions about using or configuring Unix and its utilities. [unix.se] or [su] would be better places for questions like this. – Barmar Aug 24 '21 at 22:48
  • 1
    Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods), then try something on your own. Stack Overflow isn’t a free code-writing service. – Sebastian Simon Aug 24 '21 at 22:49

1 Answers1

0

Something like this:

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);
vsemozhebuty
  • 12,992
  • 1
  • 26
  • 26