{ "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.

\n\n

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.

\n\n

If two objects share an id, their properties should be merged into a single object:

\n\n\n\n

 

\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\n\n", "translatedTitle": "根据 ID 合并两个数组", "translatedContent": "

现给定两个数组 arr1arr2 ,返回一个新的数组 joinedArray 。两个输入数组中的每个对象都包含一个 id 字段。joinedArray 是一个通过 idarr1arr2 连接而成的数组。joinedArray 的长度应为唯一值 id 的长度。返回的数组应按 id 升序 排序。

\n\n

如果一个 id 存在于一个数组中但不存在于另一个数组中,则该对象应包含在结果数组中且不进行修改。

\n\n

如果两个对象共享一个 id ,则它们的属性应进行合并:

\n\n\n\n

 

\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\n\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 4, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {Array} arr1\n * @param {Array} arr2\n * @return {Array}\n */\nvar join = function(arr1, arr2) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };\n\nfunction join(arr1: JSONValue[], arr2: JSONValue[]): JSONValue[] {\n\t\n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"2.5K\", \"totalSubmission\": \"5.1K\", \"totalAcceptedRaw\": 2450, \"totalSubmissionRaw\": 5122, \"acRate\": \"47.8%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "[{\"id\": 1,\"x\": 1},{\"id\": 2,\"x\": 9}]\n[{\"id\": 3,\"x\": 5}]", "metaData": "{\n \"name\": \"join\",\n \"params\": [\n {\n \"name\": \"arr1\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"arr2\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/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>\"],\"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" } } }