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

Commit 41ebef4

Browse files
authored
refactor: update solution with sorting via object to lc problem: No.2722 (doocs#2847)
1 parent 06533ad commit 41ebef4

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

‎solution/2700-2799/2722.Join Two Arrays by ID/README.md‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,24 @@ arr2 = [
109109
#### TypeScript
110110

111111
```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+
114116
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);
117119
} else {
118-
d.set(x.id, x);
120+
d[x.id] =x;
119121
}
120122
});
121-
return [...d.values()].sort((a, b) =>a.id-b.id);
123+
return Object.values(d);
122124
}
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>;
123130
```
124131

125132
<!-- tabs:end -->

‎solution/2700-2799/2722.Join Two Arrays by ID/README_EN.md‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,24 @@ arr2 = [
107107
#### TypeScript
108108

109109
```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+
112114
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);
115117
} else {
116-
d.set(x.id, x);
118+
d[x.id] =x;
117119
}
118120
});
119-
return [...d.values()].sort((a, b) =>a.id-b.id);
121+
return Object.values(d);
120122
}
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>;
121128
```
122129

123130
<!-- tabs:end -->
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
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+
35
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);
68
} else {
7-
d.set(x.id,x);
9+
d[x.id]=x;
810
}
911
});
10-
return [...d.values()].sort((a,b)=>a.id-b.id);
12+
return Object.values(d);
1113
}
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>;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /