Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8bbdd35

Browse files
authored
Create 2628-json-deep-equal.js
1 parent54bcc8a commit8bbdd35

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

‎javascript/2628-json-deep-equal.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
*@param {any} o1
3+
*@param {any} o2
4+
*@return {boolean}
5+
*/
6+
varareDeeplyEqual=function(o1,o2){
7+
if(o1===null||o2===null){
8+
returno1===o2;
9+
}
10+
if(typeofo1!==typeofo2){
11+
returnfalse;
12+
}
13+
if(typeofo1!=='object'){// primitives
14+
returno1===o2;
15+
}
16+
17+
if(Array.isArray(o1)&&Array.isArray(o2)){// Arrays
18+
if(o1.length!==o2.length){
19+
returnfalse;
20+
}
21+
for(leti=0;i<o1.length;i++){
22+
if(!areDeeplyEqual(o1[i],o2[i])){
23+
returnfalse;
24+
}
25+
}
26+
}elseif(!Array.isArray(o1)&&!Array.isArray(o2)){// Objects
27+
if(Object.keys(o1).length!==Object.keys(o2).length){
28+
returnfalse;
29+
}
30+
for(constkeyino1){
31+
if(!areDeeplyEqual(o1[key],o2[key])){
32+
returnfalse;
33+
}
34+
}
35+
}else{
36+
returnfalse;
37+
}
38+
returntrue;
39+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp