# 判断两个对象的值是否相等
进行对象的值是否相等的判断,包括数组
不论属性的顺序以及是否是引用,只判断两个对象是否都存在相同的属性
/**
* 判断两个对象数值是不是相等(包括数组)
* @param {object} obj1
* @param {object} obj2
* @return {boolean} boolean
*/
export function objectEqual(obj1, obj2) {
if (typeof obj1 !== 'object' || typeof obj2 !== 'object') {
throw new TypeError('parameter must typeof object')
}
const obj1Constructor = obj1.constructor
const obj2Constructor = obj2.constructor
if (obj1Constructor !== obj2Constructor) {
return false
}
if (obj1 instanceof Array) {
return ArrayEqual(obj1, obj2)
} else {
const entries1 = Object.entries(obj1)
const entries2 = Object.entries(obj2)
return ArrayEqual(entries1, entries2)
}
function ArrayEqual(arr1, arr2) {
if (JSON.stringify(arr1.sort()) == JSON.stringify(arr2.sort())) {
return true
} else {
return false
}
}
}
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
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