说我有一个功能:
function linesReverser(lines) {
var localLines = lines.slice();
localLines[1].reverse();
return _.flatten(localLines);
}
并像这样使用它:
var input = [["Hello"],["Hello", "World"]["Attention", "Please"]];
var output1 = linesReverser(input); //["Hello", "World", "Hello", "Attention", "Please"]
var output2 = linesReverser(input); //["Hello", "Hello", "World", "Attention", "Please"]
注意对象引用是如何被共享的。我是 JS 新手,但我认为复制值会缓解这个问题(line.slice()
),但它似乎不起作用。这是因为嵌套数组吗?
我怎样才能非破坏性/不变地执行反向操作?