假设我有一个这样的数组:
[1,2,3,4,5,6,7]
我怎样才能将该数组除第一个以外的所有其他数字乘以 2 所以我的新数组看起来像这样
[1,4,3,8,5,12,7]
最佳答案
您可以使用 map
和 with_index
:
[1,2,3,4,5,6,7].map.with_index{|v,i| i % 2 == 0 ? v : v * 2 }
# => [1, 4, 3, 8, 5, 12, 7]
关于ruby - 如何将数组的所有其他元素相乘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19027717/