{ "data": { "question": { "questionId": "2858", "questionFrontendId": "2722", "categoryTitle": "JavaScript", "boundTopicId": 2300585, "title": "Join Two Arrays by ID", "titleSlug": "join-two-arrays-by-id", "content": "
Given two arrays arr1
and arr2
, return a new array joinedArray
. 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 merging arr1
and arr2
based on their id
key. The length of joinedArray
should be the length of unique values of id
. The returned array should be sorted in ascending order based on the id
key.
If a given id
exists in one array but not the other, the single object with that id
should be included in the result array without modification.
If two objects share an id
, their properties should be merged into a single object:
arr2
should override the value from arr1
.\n
Example 1:
\n\n\nInput: \narr1 = [\n {"id": 1, "x": 1},\n {"id": 2, "x": 9}\n], \narr2 = [\n {"id": 3, "x": 5}\n]\nOutput: \n[\n {"id": 1, "x": 1},\n {"id": 2, "x": 9},\n {"id": 3, "x": 5}\n]\nExplanation: There are no duplicate ids so arr1 is simply concatenated with arr2.\n\n\n
Example 2:
\n\n\nInput: \narr1 = [\n {"id": 1, "x": 2, "y": 3},\n {"id": 2, "x": 3, "y": 6}\n], \narr2 = [\n {"id": 2, "x": 10, "y": 20},\n {"id": 3, "x": 0, "y": 0}\n]\nOutput: \n[\n {"id": 1, "x": 2, "y": 3},\n {"id": 2, "x": 10, "y": 20},\n {"id": 3, "x": 0, "y": 0}\n]\nExplanation: 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.\n\n\n
Example 3:
\n\n\nInput: \narr1 = [\n {"id": 1, "b": {"b": 94},"v": [4, 3], "y": 48}\n]\narr2 = [\n {"id": 1, "b": {"c": 84}, "v": [1, 3]}\n]\nOutput: [\n {"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48}\n]\nExplanation: 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.\n\n
\n
Constraints:
\n\narr1
and arr2
are valid JSON arraysarr1
and arr2
has a unique integer id
key2 <= JSON.stringify(arr1).length <= 106
2 <= JSON.stringify(arr2).length <= 106
现给定两个数组 arr1
和 arr2
,返回一个新的数组 joinedArray
。两个输入数组中的每个对象都包含一个 id
字段。joinedArray
是一个通过 id
将 arr1
和 arr2
连接而成的数组。joinedArray
的长度应为唯一值 id
的长度。返回的数组应按 id
升序 排序。
如果一个 id
存在于一个数组中但不存在于另一个数组中,则该对象应包含在结果数组中且不进行修改。
如果两个对象共享一个 id
,则它们的属性应进行合并:
arr2
中的值应覆盖 arr1
中的值。\n\n
示例 1:
\n\n\n输入:\narr1 = [\n {\"id\": 1, \"x\": 1},\n {\"id\": 2, \"x\": 9}\n], \narr2 = [\n {\"id\": 3, \"x\": 5}\n]\n输出:\n[\n {\"id\": 1, \"x\": 1},\n {\"id\": 2, \"x\": 9},\n {\"id\": 3, \"x\": 5}\n]\n解释:没有共同的 id,因此将 arr1 与 arr2 简单地连接起来。\n\n\n
示例 2:
\n\n\n输入:\narr1 = [\n {\"id\": 1, \"x\": 2, \"y\": 3},\n {\"id\": 2, \"x\": 3, \"y\": 6}\n], \narr2 = [\n {\"id\": 2, \"x\": 10, \"y\": 20},\n {\"id\": 3, \"x\": 0, \"y\": 0}\n]\n输出:\n[\n {\"id\": 1, \"x\": 2, \"y\": 3},\n {\"id\": 2, \"x\": 10, \"y\": 20},\n {\"id\": 3, \"x\": 0, \"y\": 0}\n]\n解释:id 为 1 和 id 为 3 的对象在结果数组中保持不变。id 为 2 的两个对象合并在一起。arr2 中的键覆盖 arr1 中的值。\n\n\n
示例 3:
\n\n\n输入:\narr1 = [\n {\"id\": 1, \"b\": {\"b\": 94},\"v\": [4, 3], \"y\": 48}\n]\narr2 = [\n {\"id\": 1, \"b\": {\"c\": 84}, \"v\": [1, 3]}\n]\n输出: [\n {\"id\": 1, \"b\": {\"c\": 84}, \"v\": [1, 3], \"y\": 48}\n]\n解释:具有 id 为 1 的对象合并在一起。对于键 \"b\" 和 \"v\" ,使用 arr2 中的值。由于键 \"y\" 只存在于 arr1 中,因此取 arr1 的值。\n\n
\n\n
提示:
\n\narr1 和 arr2 都是有效的 JSON 数组
在 arr1 和 arr2 中都有唯一的键值 id
2 <= JSON.stringify(arr1).length <= 106
2 <= JSON.stringify(arr2).length <= 106
\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.1.6<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[{\"id\": 1,\"x\": 1},{\"id\": 2,\"x\": 9}]\n[{\"id\": 3,\"x\": 5}]\n[{\"id\": 1,\"x\": 2,\"y\": 3},{\"id\": 2,\"x\": 3,\"y\": 6}]\n[{\"id\": 2,\"x\": 10,\"y\": 20},{\"id\": 3,\"x\": 0,\"y\": 0}]\n[{\"id\":1,\"b\":{\"b\": 94},\"v\":[4,3],\"y\":48}]\n[{\"id\":1,\"b\":{\"c\": 84},\"v\":[1,3]}]",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n