{ "data": { "question": { "questionId": "2774", "questionFrontendId": "2700", "boundTopicId": null, "title": "Differences Between Two Objects", "titleSlug": "differences-between-two-objects", "content": "

Write a function that accepts two deeply nested objects or arrays obj1 and obj2 and returns a new object representing their differences.

\n\n

The function should compare the properties of the two objects and identify any changes. The returned object should only contains keys where the value is different from obj1 to obj2. For each changed key, the value should be represented as an array [obj1 value, obj2 value]. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys. The end result should be a deeply nested object where each leaf value is a difference array.

\n\n

You may assume that both objects are the output of JSON.parse.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nobj1 = {}\nobj2 = {\n  "a": 1, \n  "b": 2\n}\nOutput: {}\nExplanation: There were no modifications made to obj1. New keys "a" and "b" appear in obj2, but keys that are added or removed should be ignored.\n
\n\n

Example 2:

\n\n
\nInput: \nobj1 = {\n  "a": 1,\n  "v": 3,\n  "x": [],\n  "z": {\n    "a": null\n  }\n}\nobj2 = {\n  "a": 2,\n  "v": 4,\n  "x": [],\n  "z": {\n    "a": 2\n  }\n}\nOutput: \n{\n  "a": [1, 2],\n  "v": [3, 4],\n  "z": {\n    "a": [null, 2]\n  }\n}\nExplanation: The keys "a", "v", and "z" all had changes applied. "a" was chnaged from 1 to 2. "v" was changed from 3 to 4. "z" had a change applied to a child object. "z.a" was changed from null to 2.\n
\n\n

Example 3:

\n\n
\nInput: \nobj1 = {\n  "a": 5, \n  "v": 6, \n  "z": [1, 2, 4, [2, 5, 7]]\n}\nobj2 = {\n  "a": 5, \n  "v": 7, \n  "z": [1, 2, 3, [1]]\n}\nOutput: \n{\n  "v": [6, 7],\n  "z": {\n    "2": [4, 3],\n    "3": {\n      "0": [2, 1]\n    }\n  }\n}\nExplanation: In obj1 and obj2, the keys "v" and "z" have different assigned values. "a" is ignored because the value is unchanged. In the key "z", there is a nested array. Arrays are treated like objects where the indices are keys. There were two alterations to the the array: z[2] and z[3][0]. z[0] and z[1] were unchanged and thus not included. z[3][1] and z[3][2] were removed and thus not included.\n
\n\n

Example 4:

\n\n
\nInput: \nobj1 = {\n  "a": {"b": 1}, \n}\nobj2 = {\n  "a": [5],\n}\nOutput: \n{\n  "a": [{"b": 1}, [5]]\n}\nExplanation: The key "a" exists in both objects. Since the two associated values have different types, they are placed in the difference array.
\n\n

Example 5:

\n\n
\nInput: \nobj1 = {\n  "a": [1, 2, {}], \n  "b": false\n}\nobj2 = {   \n  "b": false,\n  "a": [1, 2, {}]\n}\nOutput: \n{}\nExplanation: Apart from a different ordering of keys, the two objects are identical so an empty object is returned.
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 110, "dislikes": 17, "isLiked": null, "similarQuestions": "[{\"title\": \"JSON Deep Equal\", \"titleSlug\": \"json-deep-equal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Convert Object to JSON String\", \"titleSlug\": \"convert-object-to-json-string\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "exampleTestcases": "{}\n{\"a\": 1, \"b\": 2}\n{\"a\": 1, \"v\": 3, \"x\": [], \"z\": {\"a\": null}}\n{\"a\": 2, \"v\": 4, \"x\": [], \"z\": {\"a\": 2}}\n{\"a\": 5, \"v\": 6, \"z\": [1,2,4, [2,5,7]]}\n{\"a\": 5, \"v\": 7, \"z\": [1,2,3, [1]]}\n{\"a\":{\"b\":1}}\n{\"a\":[5]}\n{\"a\": [1, 2, {}],\"b\": false}\n{\"b\": false, \"a\": [1, 2, {}]}", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {object} obj1\n * @param {object} obj2\n * @return {object}\n */\nfunction objDiff(obj1, obj2) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function objDiff(obj1: any, obj2: any): any {\n\n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"4.6K\", \"totalSubmission\": \"5.5K\", \"totalAcceptedRaw\": 4567, \"totalSubmissionRaw\": 5469, \"acRate\": \"83.5%\"}", "hints": [ "Find the intersection of the keys/indices on the two arrays/objects.", "Analyze the data structure recursively.", "For each key in the intersection, omit if there are no differences in the leaves. Otherwise return the difference." ], "solution": { "id": "1914", "canSeeDetail": true, "paidOnly": false, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "{}\n{\"a\": 1, \"b\": 2}", "metaData": "{\n \"name\": \"objDiff\",\n \"params\": [\n {\n \"name\": \"obj1\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"obj2\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 4.5.4, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }