1

I am trying to find element in target array and update in the source array.

let sourceArray = [
  {
    "userId": "123",
    "applicationId": "abc",
    "selections": [
      {
        "applicationId": 70930,
        "creationDate": "2021-01-28",
        "responseStatus": "PENDING"
      }
    ]
  }
]

let array2 = [
  {
      "applicationId": 70930,
      "code": "TEST CODE",
      "creationDate": "2021-01-28",
      "submissionDate": "2021-01-29",
      "status": "SUBMITTED",
      "outcomeStatus": "PENDING",
      "responseStatus": "PENDING"
  }
]

My expected outcome is selections to get updated in source array with matching elements from array2 based on applicationId

[
  {
    "userId": "123",
    "applicationId": "abc",
    "selections": [
      {
        "applicationId": 70930,
        "code": "TEST CODE",
        "creationDate": "2021-01-28",
        "submissionDate": "2021-01-29",
        "status": "SUBMITTED",
        "outcomeStatus": "PENDING",
        "responseStatus": "PENDING"
      }
    ]
  }
]

I tried updating the array using code below

const newArray = sourceArray.map(item => {
  let item2 = array2.find(i2 => item.selections.some(id => i2.applicationId === id.applicationId));
  return item2 ? { ...item, ...item2 } : item;
});
3
  • So if array2 has creationDate equal to 2021-02-01, will it override what's in sourceArray? Commented Feb 2, 2021 at 4:33
  • Yes it will update the sourceArray Commented Feb 2, 2021 at 4:35
  • I have updated my attempt, I keep getting back, it should go in selections. [{ "userId": "123", "applicationId": 70930, "selections": [ { "applicationId": 70930, "creationDate": "2021-01-28", "responseStatus": "PENDING" } ], "code": "TEST CODE", "creationDate": "2021-01-28", "submissionDate": "2021-01-29", "status": "SUBMITTED", "outcomeStatus": "PENDING", "responseStatus": "PENDING" }] Commented Feb 2, 2021 at 4:36

1 Answer 1

2

You could try this. Just remember to keep track of other info when destructing

let sourceArray = [
  {
    userId: "123",
    applicationId: "abc",
    selections: [
      {
        applicationId: 70930,
        creationDate: "2021-01-28",
        responseStatus: "PENDING",
      },
    ],
  },
];

let array2 = [
  {
    applicationId: 70930,
    code: "TEST CODE",
    creationDate: "2021-01-28",
    submissionDate: "2021-01-29",
    status: "SUBMITTED",
    outcomeStatus: "PENDING",
    responseStatus: "PENDING",
  },
];

sourceArray = sourceArray.map(({ selections, ...otherInfo }) => ({
  ...otherInfo,
  selections: selections.map((selection) => ({
    ...selection,
    ...array2.find((el) => el.applicationId === selection.applicationId),
  })),
}));

console.log(sourceArray)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.