I need to update the state in one array based on the values of another array. The goal is to include all statuses from the second array into the first without any duplicates. However, the provided sample code is not comprehensive.
const arr1 = [{ agentId: 1234, state: "CA" }];
const arr2 = [{ agentId: 1234, AK: "c", AL: "N", CA: "c" }];
var res = [];
arr1.forEach((x) => {
arr2.forEach((y) => {
if (x.agentId === y.agentId) {
for (const [key, value] of Object.entries(y)) {
if (value === "c") {
x.state += " ," + key;
}
}
}
});
});
The desired output should be as follows:
arr1 = [{ agentId: 1234, state: "CA, AK" }]