{ "data": { "question": { "questionId": "2735", "questionFrontendId": "2628", "categoryTitle": "JavaScript", "boundTopicId": 2222277, "title": "JSON Deep Equal", "titleSlug": "json-deep-equal", "content": "
Given two objects o1
and o2
, check if they are deeply equal.
For two objects to be deeply equal, they must contain the same keys, and the associated values must also be deeply equal. Two objects are also considered deeply equal if they pass the ===
equality check.
You may assume both objects are the output of JSON.parse
. In other words, they are valid JSON.
Please solve it without using lodash's _.isEqual()
function.
\n
Example 1:
\n\n\nInput: o1 = {"x":1,"y":2}, o2 = {"x":1,"y":2}\nOutput: true\nExplanation: The keys and values match exactly.\n\n\n
Example 2:
\n\n\nInput: o1 = {"y":2,"x":1}, o2 = {"x":1,"y":2}\nOutput: true\nExplanation: Although the keys are in a different order, they still match exactly.\n\n\n
Example 3:
\n\n\nInput: o1 = {"x":null,"L":[1,2,3]}, o2 = {"x":null,"L":["1","2","3"]}\nOutput: false\nExplanation: The array of numbers is different from the array of strings.\n\n\n
Example 4:
\n\n\nInput: o1 = true, o2 = false\nOutput: false\nExplanation: true !== false\n\n
\n
Constraints:
\n\n1 <= JSON.stringify(o1).length <= 105
1 <= JSON.stringify(o2).length <= 105
maxNestingDepth <= 1000
给定两个对象 o1
和 o2
,请你检查它们是否 完全相等 。
对于两个 完全相等 的对象,它们必须包含相同的键,并且相关的值也必须 完全相等 。如果两个对象通过了 ===
相等性检查,它们也被认为是 完全相等 的。
你可以假设这两个对象都是 JSON.parse
的输出。换句话说,它们是有效的 JSON
。
请你在不使用 lodash 的 _.isEqual()
函数的前提下解决这个问题。
\n\n
示例 1:
\n\n\n输入:o1 = {\"x\":1,\"y\":2}, o2 = {\"x\":1,\"y\":2}\n输出:true\n输入:键和值完全匹配。\n\n\n
示例 2:
\n\n\n输入:o1 = {\"y\":2,\"x\":1}, o2 = {\"x\":1,\"y\":2}\n输出:true\n解释:尽管键的顺序不同,但它们仍然完全匹配。\n\n\n
示例 3:
\n\n\n输入:o1 = {\"x\":null,\"L\":[1,2,3]}, o2 = {\"x\":null,\"L\":[\"1\",\"2\",\"3\"]}\n输出:false\n解释:数字数组不同于字符串数组。\n\n\n
示例 4:
\n\n\n输入:o1 = true, o2 = false\n输出:false\n解释:true !== false\n\n
\n\n
提示:
\n\n1 <= JSON.stringify(o1).length <= 105
1 <= JSON.stringify(o2).length <= 105
maxNestingDepth <= 1000
\\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 4.5.4<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/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": "{\"x\":1,\"y\":2}\n{\"x\":1,\"y\":2}\n{\"y\":2,\"x\":1}\n{\"x\":1,\"y\":2}\n{\"x\":null,\"L\":[1,2,3]}\n{\"x\":null,\"L\":[\"1\",\"2\",\"3\"]}\ntrue\nfalse",
"__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