File tree 3 files changed +39
-18
lines changed
solution/2700-2799/2722.Join Two Arrays by ID
3 files changed +39
-18
lines changed Original file line number Diff line number Diff line change @@ -109,17 +109,24 @@ arr2 = [
109
109
#### TypeScript
110
110
111
111
``` ts
112
- function join(arr1 : any [], arr2 : any []): any [] {
113
- const d = new Map (arr1 .map (x => [x .id , x ]));
112
+ function join(arr1 : ArrayType [], arr2 : ArrayType []): ArrayType [] {
113
+ const r = (acc : Obj , x : ArrayType ): Obj => ((acc [x .id ] = x ), acc );
114
+ const d = arr1 .reduce (r , {});
115
+
114
116
arr2 .forEach (x => {
115
- if (d . has ( x .id ) ) {
116
- d . set ( x . id , { ... d . get ( x .id ), ... x } );
117
+ if (d [ x .id ] ) {
118
+ Object . assign ( d [ x .id ], x );
117
119
} else {
118
- d . set ( x .id , x ) ;
120
+ d [ x .id ] = x ;
119
121
}
120
122
});
121
- return [ ... d . values ()]. sort (( a , b ) => a . id - b . id );
123
+ return Object . values (d );
122
124
}
125
+
126
+ type JSONValue = null | boolean | number | string | JSONValue [] | { [key : string ]: JSONValue };
127
+ type ArrayType = { id: number } & Record <string , JSONValue >;
128
+
129
+ type Obj = Record <number , ArrayType >;
123
130
```
124
131
125
132
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -107,17 +107,24 @@ arr2 = [
107
107
#### TypeScript
108
108
109
109
``` ts
110
- function join(arr1 : any [], arr2 : any []): any [] {
111
- const d = new Map (arr1 .map (x => [x .id , x ]));
110
+ function join(arr1 : ArrayType [], arr2 : ArrayType []): ArrayType [] {
111
+ const r = (acc : Obj , x : ArrayType ): Obj => ((acc [x .id ] = x ), acc );
112
+ const d = arr1 .reduce (r , {});
113
+
112
114
arr2 .forEach (x => {
113
- if (d . has ( x .id ) ) {
114
- d . set ( x . id , { ... d . get ( x .id ), ... x } );
115
+ if (d [ x .id ] ) {
116
+ Object . assign ( d [ x .id ], x );
115
117
} else {
116
- d . set ( x .id , x ) ;
118
+ d [ x .id ] = x ;
117
119
}
118
120
});
119
- return [ ... d . values ()]. sort (( a , b ) => a . id - b . id );
121
+ return Object . values (d );
120
122
}
123
+
124
+ type JSONValue = null | boolean | number | string | JSONValue [] | { [key : string ]: JSONValue };
125
+ type ArrayType = { id: number } & Record <string , JSONValue >;
126
+
127
+ type Obj = Record <number , ArrayType >;
121
128
```
122
129
123
130
<!-- tabs:end -->
Original file line number Diff line number Diff line change 1
- function join ( arr1 : any [ ] , arr2 : any [ ] ) : any [ ] {
2
- const d = new Map ( arr1 . map ( x => [ x . id , x ] ) ) ;
1
+ function join ( arr1 : ArrayType [ ] , arr2 : ArrayType [ ] ) : ArrayType [ ] {
2
+ const r = ( acc : Obj , x : ArrayType ) : Obj => ( ( acc [ x . id ] = x ) , acc ) ;
3
+ const d = arr1 . reduce ( r , { } ) ;
4
+
3
5
arr2 . forEach ( x => {
4
- if ( d . has ( x . id ) ) {
5
- d . set ( x . id , { ... d . get ( x . id ) , ... x } ) ;
6
+ if ( d [ x . id ] ) {
7
+ Object . assign ( d [ x . id ] , x ) ;
6
8
} else {
7
- d . set ( x . id , x ) ;
9
+ d [ x . id ] = x ;
8
10
}
9
11
} ) ;
10
- return [ ... d . values ( ) ] . sort ( ( a , b ) => a . id - b . id ) ;
12
+ return Object . values ( d ) ;
11
13
}
14
+
15
+ type JSONValue = null | boolean | number | string | JSONValue [ ] | { [ key : string ] : JSONValue } ;
16
+ type ArrayType = { id : number } & Record < string , JSONValue > ;
17
+
18
+ type Obj = Record < number , ArrayType > ;
You can’t perform that action at this time.
0 commit comments