-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2628-JSONDeepEqual.js
More file actions
81 lines (68 loc) · 3.05 KB
/
2628-JSONDeepEqual.js
File metadata and controls
81 lines (68 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 2628. JSON Deep Equal
// Given two values o1 and o2, return a boolean value indicating whether two values, o1 and o2, are deeply equal.
// For two values to be deeply equal, the following conditions must be met:
// If both values are primitive types, they are deeply equal if they pass the === equality check.
// If both values are arrays, they are deeply equal if they have the same elements in the same order, and each element is also deeply equal according to these conditions.
// If both values are objects, they are deeply equal if they have the same keys, and the associated values for each key are also deeply equal according to these conditions.
// You may assume both values are the output of JSON.parse. In other words, they are valid JSON.
// Please solve it without using lodash's _.isEqual() function
// Example 1:
// Input: o1 = {"x":1,"y":2}, o2 = {"x":1,"y":2}
// Output: true
// Explanation: The keys and values match exactly.
// Example 2:
// Input: o1 = {"y":2,"x":1}, o2 = {"x":1,"y":2}
// Output: true
// Explanation: Although the keys are in a different order, they still match exactly.
// Example 3:
// Input: o1 = {"x":null,"L":[1,2,3]}, o2 = {"x":null,"L":["1","2","3"]}
// Output: false
// Explanation: The array of numbers is different from the array of strings.
// Example 4:
// Input: o1 = true, o2 = false
// Output: false
// Explanation: true !== false
// Constraints:
// 1 <= JSON.stringify(o1).length <= 10^5
// 1 <= JSON.stringify(o2).length <= 10^5
// maxNestingDepth <= 1000
/**
* @param {null|boolean|number|string|Array|Object} o1
* @param {null|boolean|number|string|Array|Object} o2
* @return {boolean}
*/
var areDeeplyEqual = function(o1, o2) {
// 类型不同 直接返回 false
if (Object.prototype.toString.call(o1) !== Object.prototype.toString.call(o2)) {
return false;
}
// 数组和对象判断
if (o1 !== null && typeof o1 === 'object') {
// 判断 key 数量相同 && key 值都相同
return Object.keys(o1).length === Object.keys(o2).length &&
Object.entries(o1).every(
// 如果是对象递归判断
([key, value]) => areDeeplyEqual(value, o2[key])
)
}
// 基本类型判断
return o1 === o2;
};
// Input: o1 = {"x":1,"y":2}, o2 = {"x":1,"y":2}
// Output: true
// Explanation: The keys and values match exactly.
// Example 2:
// Input: o1 = {"y":2,"x":1}, o2 = {"x":1,"y":2}
// Output: true
// Explanation: Although the keys are in a different order, they still match exactly.
// Example 3:
// Input: o1 = {"x":null,"L":[1,2,3]}, o2 = {"x":null,"L":["1","2","3"]}
// Output: false
// Explanation: The array of numbers is different from the array of strings.
// Example 4:
// Input: o1 = true, o2 = false
// Output: false
console.log(areDeeplyEqual({"x":1,"y":2},{"x":1,"y":2})); // true
console.log(areDeeplyEqual({"x":1,"y":2},{"y":2,"x":1})); // true
console.log(areDeeplyEqual({"x":null,"L":[1,2,3]},{"x":null,"L":["1","2","3"]})); // false
console.log(areDeeplyEqual(true,false)); // false