有朋友问我用Ruby最好、最高效的方式来达到JavaScript的效果splice
Ruby 中的方法。
这意味着不对数组本身或副本进行迭代。
“从索引开始处开始,删除长度项并(可选)插入元素。最后在数组中返回删除的项。” 这是误导性的,请参阅下面的 JS 示例。
http://www.mennovanslooten.nl/blog/post/41
没有的快速破解optional代换:
from_index = 2
for_elements = 2
sostitute_with = :test
initial_array = [:a, :c, :h, :g, :t, :m]
# expected result: [:a, :c, :test, :t, :m]
initial_array[0..from_index-1] + [sostitute_with] + initial_array[from_index + for_elements..-1]
你的是啥呢?
一根线比较好。
Update:
// JavaScript
var a = ['a', 'c', 'h', 'g', 't', 'm'];
var b = a.splice(2, 2, 'test');
> b is now ["h", "g"]
> a is now ["a", "c", "test", "t", "m"]
我需要结果“a”数组,而不是“b”。