回答編集履歴
1
追記
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
```
|
13
|
+
```json
|
14
14
|
|
15
15
|
{
|
16
16
|
|
@@ -39,6 +39,8 @@
|
|
39
39
|
let m;
|
40
40
|
|
41
41
|
if( m = prop.match(/^(sample\d+)_(.*)/) ) { // 文字列処理(正規表現)
|
42
|
+
|
43
|
+
//console.log(m)
|
42
44
|
|
43
45
|
let [_, grp, key] = m;
|
44
46
|
|
@@ -73,3 +75,77 @@
|
|
73
75
|
|
74
76
|
|
75
77
|
※要件をまとめるコツは、リファレンスや他者の質疑などの**活字**を真似て、どのような言い回しが**幅広い年齢に伝わるのか**を推敲し、箇条書きにすることです。
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
追記)
|
84
|
+
|
85
|
+
もう一案
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
```json
|
90
|
+
|
91
|
+
{
|
92
|
+
|
93
|
+
"sample4":{"number":4,"name":"リンゴ","life":1,"five":"1個"},
|
94
|
+
|
95
|
+
"sample21":{"number":21,"name":"バナナ","life":5,"five":"2個"}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
```
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
```javascript
|
104
|
+
|
105
|
+
var data = datas[0];
|
106
|
+
|
107
|
+
var tmp = {};
|
108
|
+
|
109
|
+
for( var prop in data ) {
|
110
|
+
|
111
|
+
let m;
|
112
|
+
|
113
|
+
if( m = prop.match(/^(sample(\d+))_(.*)/) ) { // number に相当する箇所を抽出
|
114
|
+
|
115
|
+
//console.log(m)
|
116
|
+
|
117
|
+
let [_, grp, num, key] = m;
|
118
|
+
|
119
|
+
if( !tmp[grp] ) tmp[grp] = {};
|
120
|
+
|
121
|
+
if( key==="number" ) {
|
122
|
+
|
123
|
+
tmp[grp][key] = num|0;
|
124
|
+
|
125
|
+
tmp[grp].name = data[prop].value;
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
else {
|
130
|
+
|
131
|
+
tmp[grp][key] = data[prop].value;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
console.log( JSON.stringify( tmp ) );
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
var rslt;
|
144
|
+
|
145
|
+
Object.values(tmp).some( item=>item.name==="バナナ"&&(rslt=item) );
|
146
|
+
|
147
|
+
console.log( rslt );
|
148
|
+
|
149
|
+
// => {"number":21,"name":"バナナ","life":5,"five":"2個"}
|
150
|
+
|
151
|
+
```
|