2722. Join Two Arrays by ID
Tags
- JSON
Link
Question
Given two arrays
arr1
andarr2
, return a new arrayjoinedArray
. All the objects in each of the two inputs arrays will contain an id field that has an integer value.
joinedArray
is an array formed by mergingarr1
andarr2
based on theirid
key. The length ofjoinedArray
should be the length of unique values ofid
. The returned array should be sorted in ascending order based on theid
key.If a given
id
exists in one array but not the other, the single object with thatid
should be included in the result array without modification.If two objects share an
id
, their properties should be merged into a single object:
- If a key only exists in one object, that single key-value pair should be included in the object.
- If a key is included in both objects, the value in the object from arr2 should override the value from arr1.
Example 1:
Input:
arr1 = [
{"id": 1, "x": 1},
{"id": 2, "x": 9}
],
arr2 = [
{"id": 3, "x": 5}
]
Output:
[
{"id": 1, "x": 1},
{"id": 2, "x": 9},
{"id": 3, "x": 5}
]
Explanation: There are no duplicate ids so arr1 is simply concatenated with arr2.
Example 2:
Input:
arr1 = [
{"id": 1, "x": 2, "y": 3},
{"id": 2, "x": 3, "y": 6}
],
arr2 = [
{"id": 2, "x": 10, "y": 20},
{"id": 3, "x": 0, "y": 0}
]
Output:
[
{"id": 1, "x": 2, "y": 3},
{"id": 2, "x": 10, "y": 20},
{"id": 3, "x": 0, "y": 0}
]
Explanation: The two objects with id=1 and id=3 are included in the result array without modifiction. The two objects with id=2 are merged together. The keys from arr2 override the values in arr1.
Example 3:
Input:
arr1 = [
{"id": 1, "b": {"b": 94},"v": [4, 3], "y": 48}
]
arr2 = [
{"id": 1, "b": {"c": 84}, "v": [1, 3]}
]
Output: [
{"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48}
]
Explanation: The two objects with id=1 are merged together. For the keys "b" and "v" the values from arr2 are used. Since the key "y" only exists in arr1, that value is taken form arr1.
Constraints:
arr1
andarr2
are valid JSON arrays- Each object in
arr1
andarr2
has a unique integerid
key 2 <= JSON.stringify(arr1).length <= 10
62 <= JSON.stringify(arr2).length <= 10
6
Answer
JavaScript
/**
* @param {Array} arr1
* @param {Array} arr2
* @return {Array}
*/
var join = function (arr1, arr2) {
// Create a map to store objects by their id
const map = new Map();
// Add objects from arr1 to the map
for (const obj of arr1) {
map.set(obj.id, obj);
}
// Merge objects from arr2 into the map
for (const obj of arr2) {
if (map.has(obj.id)) {
// Merge the two objects, prioritizing arr2
map.set(obj.id, { ...map.get(obj.id), ...obj });
} else {
// Add the new object from arr2
map.set(obj.id, obj);
}
}
// Convert the map's values to an array and sort by id
return Array.from(map.values()).sort((a, b) => a.id - b.id);
};
/**
* @param {Array} arr1
* @param {Array} arr2
* @return {Array}
*/
var join = function (arr1, arr2) {
let res = {};
for (let i = 0; i < arr1.length; i++) {
res[arr1[i].id] = arr1[i];
}
for (let obj2 of arr2) {
if (res[obj2.id]) Object.assign(res[obj2.id], obj2);
else res[obj2.id] = obj2;
}
return Object.values(res);
};