回答編集履歴
4
hairetu
test
CHANGED
@@ -97,3 +97,35 @@
|
|
97
97
|
console.log(userRank);
|
98
98
|
|
99
99
|
```
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
# 配列から
|
104
|
+
|
105
|
+
やってることは対して変わりません
|
106
|
+
|
107
|
+
(一応同順あり版で)
|
108
|
+
|
109
|
+
```javascript
|
110
|
+
|
111
|
+
const userData = {
|
112
|
+
|
113
|
+
"a": [0, 0, 0, 23, 0],
|
114
|
+
|
115
|
+
"b": [0, 0, 0, 31, 0],
|
116
|
+
|
117
|
+
"c": [0, 0, 0, 4, 0],
|
118
|
+
|
119
|
+
"d": [0, 0, 0, 23, 0],
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
const userRank = Object.entries(userData)
|
124
|
+
|
125
|
+
.map((x,y)=>[x[0],Object.entries(userData).filter(z=>z[1][3]>x[1][3]).length+1])
|
126
|
+
|
127
|
+
.reduce((x,y)=>(x[y[0]]=y[1],x),{});
|
128
|
+
|
129
|
+
console.log(userRank);
|
130
|
+
|
131
|
+
```
|
3
chousei
test
CHANGED
@@ -69,3 +69,31 @@
|
|
69
69
|
console.log(userRank);
|
70
70
|
|
71
71
|
```
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
# 同順あり
|
76
|
+
|
77
|
+
```javascript
|
78
|
+
|
79
|
+
const userData = {
|
80
|
+
|
81
|
+
"a": 23,
|
82
|
+
|
83
|
+
"b": 31,
|
84
|
+
|
85
|
+
"c": 4,
|
86
|
+
|
87
|
+
"d": 23,
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
const userRank = Object.entries(userData)
|
92
|
+
|
93
|
+
.map((x,y)=>[x[0],Object.entries(userData).filter(z=>z[1]>x[1]).length+1])
|
94
|
+
|
95
|
+
.reduce((x,y)=>(x[y[0]]=y[1],x),{});
|
96
|
+
|
97
|
+
console.log(userRank);
|
98
|
+
|
99
|
+
```
|
2
chousei
test
CHANGED
@@ -39,3 +39,33 @@
|
|
39
39
|
res=res.map((x,y)=>[(y+1).toString()+"位",x[0]+"さん",x[1].toString()+"p"]);//各要素に単位や敬称をつける
|
40
40
|
|
41
41
|
```
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
# rankをオブジェクトにする
|
46
|
+
|
47
|
+
```javascript
|
48
|
+
|
49
|
+
const userData = {
|
50
|
+
|
51
|
+
"a": 23,
|
52
|
+
|
53
|
+
"b": 31,
|
54
|
+
|
55
|
+
"c": 4
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
const userRank = Object
|
60
|
+
|
61
|
+
.entries(userData)
|
62
|
+
|
63
|
+
.sort((x,y)=>-x[1]+y[1])
|
64
|
+
|
65
|
+
.map((x,y)=>[x[0],y+1])
|
66
|
+
|
67
|
+
.reduce((x,y)=>(x[y[0]]=y[1],x),{});
|
68
|
+
|
69
|
+
console.log(userRank);
|
70
|
+
|
71
|
+
```
|
1
ちょうせい
test
CHANGED
@@ -10,8 +10,32 @@
|
|
10
10
|
|
11
11
|
}
|
12
12
|
|
13
|
-
var res=Object.entries(userData).sort((x,y)=>-x[1]+y[1]).map((x,y)=>[y+1].concat(x));
|
13
|
+
//var res=Object.entries(userData).sort((x,y)=>-x[1]+y[1]).map((x,y)=>[y+1].concat(x));
|
14
|
+
|
15
|
+
var res=Object.entries(userData)
|
16
|
+
|
17
|
+
.sort((x,y)=>-x[1]+y[1])
|
18
|
+
|
19
|
+
.map((x,y)=>[(y+1).toString()+"位",x[0]+"さん",x[1].toString()+"p"]);
|
14
20
|
|
15
21
|
console.log(res);
|
16
22
|
|
17
23
|
```
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
一部調整しました。
|
28
|
+
|
29
|
+
上記ソースを3段階に分割すると以下
|
30
|
+
|
31
|
+
# 解説
|
32
|
+
|
33
|
+
```javascript
|
34
|
+
|
35
|
+
var res=Object.entries(userData); // [["a",23],["b",31],["c",4]]を抽出
|
36
|
+
|
37
|
+
res=res.sort((x,y)=>-x[1]+y[1]); // ソートして[["b",31],["a",23],["c",4]]にする
|
38
|
+
|
39
|
+
res=res.map((x,y)=>[(y+1).toString()+"位",x[0]+"さん",x[1].toString()+"p"]);//各要素に単位や敬称をつける
|
40
|
+
|
41
|
+
```
|