File tree Expand file tree Collapse file tree 6 files changed +74
-0
lines changed
2700.Differences Between Two Objects
2703.Return Length of Arguments Passed Expand file tree Collapse file tree 6 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -137,7 +137,25 @@ obj2 = {
137
137
<!-- 这里可写当前语言的特殊实现逻辑 -->
138
138
139
139
``` ts
140
+ function objDiff(obj1 : any , obj2 : any ): any {
141
+ if (type (obj1 ) !== type (obj2 )) return [obj1 , obj2 ];
142
+ if (! isObject (obj1 )) return obj1 === obj2 ? {} : [obj1 , obj2 ];
143
+ const diff: Record <string , unknown > = {};
144
+ const sameKeys = Object .keys (obj1 ).filter (key => key in obj2 );
145
+ sameKeys .forEach (key => {
146
+ const subDiff = objDiff (obj1 [key ], obj2 [key ]);
147
+ if (Object .keys (subDiff ).length ) diff [key ] = subDiff ;
148
+ });
149
+ return diff ;
150
+ }
151
+
152
+ function type(obj : unknown ): string {
153
+ return Object .prototype .toString .call (obj ).slice (8 , - 1 );
154
+ }
140
155
156
+ function isObject(obj : unknown ): obj is Record <string , unknown > {
157
+ return typeof obj === ' object' && obj !== null ;
158
+ }
141
159
```
142
160
143
161
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -129,7 +129,25 @@ obj2 = {
129
129
### ** TypeScript**
130
130
131
131
``` ts
132
+ function objDiff(obj1 : any , obj2 : any ): any {
133
+ if (type (obj1 ) !== type (obj2 )) return [obj1 , obj2 ];
134
+ if (! isObject (obj1 )) return obj1 === obj2 ? {} : [obj1 , obj2 ];
135
+ const diff: Record <string , unknown > = {};
136
+ const sameKeys = Object .keys (obj1 ).filter (key => key in obj2 );
137
+ sameKeys .forEach (key => {
138
+ const subDiff = objDiff (obj1 [key ], obj2 [key ]);
139
+ if (Object .keys (subDiff ).length ) diff [key ] = subDiff ;
140
+ });
141
+ return diff ;
142
+ }
143
+
144
+ function type(obj : unknown ): string {
145
+ return Object .prototype .toString .call (obj ).slice (8 , - 1 );
146
+ }
132
147
148
+ function isObject(obj : unknown ): obj is Record <string , unknown > {
149
+ return typeof obj === ' object' && obj !== null ;
150
+ }
133
151
```
134
152
135
153
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ function objDiff ( obj1 : any , obj2 : any ) : any {
2
+ if ( type ( obj1 ) !== type ( obj2 ) ) return [ obj1 , obj2 ] ;
3
+ if ( ! isObject ( obj1 ) ) return obj1 === obj2 ? { } : [ obj1 , obj2 ] ;
4
+ const diff : Record < string , unknown > = { } ;
5
+ const sameKeys = Object . keys ( obj1 ) . filter ( key => key in obj2 ) ;
6
+ sameKeys . forEach ( key => {
7
+ const subDiff = objDiff ( obj1 [ key ] , obj2 [ key ] ) ;
8
+ if ( Object . keys ( subDiff ) . length ) diff [ key ] = subDiff ;
9
+ } ) ;
10
+ return diff ;
11
+ }
12
+
13
+ function type ( obj : unknown ) : string {
14
+ return Object . prototype . toString . call ( obj ) . slice ( 8 , - 1 ) ;
15
+ }
16
+
17
+ function isObject ( obj : unknown ) : obj is Record < string , unknown > {
18
+ return typeof obj === 'object' && obj !== null ;
19
+ }
Original file line number Diff line number Diff line change @@ -50,7 +50,13 @@ Three values were passed to the function so it should return 3.
50
50
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
51
52
52
``` ts
53
+ function argumentsLength(... args : any []): number {
54
+ return args .length ;
55
+ }
53
56
57
+ /**
58
+ * argumentsLength(1, 2, 3); // 3
59
+ */
54
60
```
55
61
56
62
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -44,7 +44,13 @@ Three values were passed to the function so it should return 3.
44
44
### ** TypeScript**
45
45
46
46
``` ts
47
+ function argumentsLength(... args : any []): number {
48
+ return args .length ;
49
+ }
47
50
51
+ /**
52
+ * argumentsLength(1, 2, 3); // 3
53
+ */
48
54
```
49
55
50
56
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ function argumentsLength ( ...args : any [ ] ) : number {
2
+ return args . length ;
3
+ }
4
+
5
+ /**
6
+ * argumentsLength(1, 2, 3); // 3
7
+ */
You can’t perform that action at this time.
0 commit comments