回答編集履歴

6

修正

2017/07/13 05:59

投稿

退会済みユーザー
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  new_h_array = h_array.uniq.group_by { |e| e[:name] }.map do |_k, v|
12
12
 
13
- pos = v.index { |e| e[:checked] }
13
+ pos = v.index { |i| i[:checked] }
14
14
 
15
15
  pos.nil? ? v.first : v[pos]
16
16
 

5

修正

2017/07/13 05:58

投稿

退会済みユーザー
test CHANGED
@@ -10,7 +10,9 @@
10
10
 
11
11
  new_h_array = h_array.uniq.group_by { |e| e[:name] }.map do |_k, v|
12
12
 
13
- v.index { |e| e[:checked] }.nil? ? v.first : v[v.index { |e| e[:checked] }]
13
+ pos = v.index { |e| e[:checked] }
14
+
15
+ pos.nil? ? v.first : v[pos]
14
16
 
15
17
  end
16
18
 

4

修正

2017/07/13 05:57

投稿

退会済みユーザー
test CHANGED
@@ -10,17 +10,7 @@
10
10
 
11
11
  new_h_array = h_array.uniq.group_by { |e| e[:name] }.map do |_k, v|
12
12
 
13
- if v.size > 1
14
-
15
- pos = v.map.with_index { |e, i| i if e[:checked] }.compact.first
13
+ v.index { |e| e[:checked] }.nil? ? v.first : v[v.index { |e| e[:checked] }]
16
-
17
- v[pos]
18
-
19
- else
20
-
21
- v.first
22
-
23
- end
24
14
 
25
15
  end
26
16
 

3

修正

2017/07/13 05:56

投稿

退会済みユーザー
test CHANGED
@@ -5,8 +5,6 @@
5
5
 
6
6
 
7
7
  h_array = [{:name=>"taro", :checked=>true}, {:name=>"jiro", :checked=>true}, {:name=>"taro", :checked=>false},{:name=>"hanako", :checked=>false},{:name=>"hanako", :checked=>false}]
8
-
9
-
10
8
 
11
9
 
12
10
 
@@ -35,3 +33,5 @@
35
33
 
36
34
 
37
35
  ```
36
+
37
+ 考えてみました。

2

修正

2017/07/13 05:49

投稿

退会済みユーザー
test CHANGED
@@ -10,11 +10,11 @@
10
10
 
11
11
 
12
12
 
13
- new_h_array = h_array.uniq.group_by { |e| e[:name] }.map do |k, v|
13
+ new_h_array = h_array.uniq.group_by { |e| e[:name] }.map do |_k, v|
14
14
 
15
15
  if v.size > 1
16
16
 
17
- pos = v.map.with_index{|e, i| i if e[:checked]}.compact.first
17
+ pos = v.map.with_index { |e, i| i if e[:checked] }.compact.first
18
18
 
19
19
  v[pos]
20
20
 
@@ -30,4 +30,8 @@
30
30
 
31
31
  p new_h_array
32
32
 
33
+ # => [{:name=>"taro", :checked=>true}, {:name=>"jiro", :checked=>true}, {:name=>"hanako", :checked=>false}]
34
+
35
+
36
+
33
37
  ```

1

修正

2017/07/13 05:39

投稿

退会済みユーザー
test CHANGED
@@ -1,4 +1,8 @@
1
1
  ```Ruby
2
+
3
+ # encoding: utf-8
4
+
5
+
2
6
 
3
7
  h_array = [{:name=>"taro", :checked=>true}, {:name=>"jiro", :checked=>true}, {:name=>"taro", :checked=>false},{:name=>"hanako", :checked=>false},{:name=>"hanako", :checked=>false}]
4
8
 
@@ -6,6 +10,24 @@
6
10
 
7
11
 
8
12
 
13
+ new_h_array = h_array.uniq.group_by { |e| e[:name] }.map do |k, v|
14
+
15
+ if v.size > 1
16
+
17
+ pos = v.map.with_index{|e, i| i if e[:checked]}.compact.first
18
+
19
+ v[pos]
20
+
21
+ else
22
+
23
+ v.first
24
+
25
+ end
26
+
27
+ end
28
+
29
+
30
+
9
- p h_array.uniq
31
+ p new_h_array
10
32
 
11
33
  ```