a = [[5, 6], [6, 5], [7, 4], [1, 0, 0], [9, 9], [6, 8], [8, 6], [1, 8, 0], [9, 0]]
如何对内部数组中的整数求和并返回内部数组之和的数组?
我需要它返回[(5+6), (6+5), (7+4), (1+0+0), ...]
,因此[11, 11, 11, 1, 18, 14, 14, 9, 9]
答案 0 :(得分:5)
a.map{|a| a.inject(:+)}
# => [11, 11, 11, 1, 18, 14, 14, 9, 9]
答案 1 :(得分:5)
尝试
a.map { |a| a.reduce(:+) }
然后sum_arry = a.map { |sub_arry| sub_arry.inject(&:+) }
应该是您想要的数组。
答案 2 :(得分:4)
使用// Get a list of posts starred
... databaseReference.child("users").child(getUid()).child(starred);
// Iterate through and get value of `author` and `post`
...
// Store the values in variables
var author = "...";
var post = "...";
// Make the call
return databaseReference.child("posts").child(post);
+ map
reduce