有人能解释一下下面关于JavaScript的concat()和slice()方法的例子吗?

有人能解释一下下面关于JavaScript的concat()和slice()方法的例子吗?,javascript,arrays,slice,concat,Javascript,Arrays,Slice,Concat,我理解concat和slice方法,但我似乎不能理解这个例子,请帮助 function remove(array, index) {   return array.slice(0, index)     .concat(array.slice(index + 1)); } console.log(remove(["a", "b", "c", "d", "e"], 2)); // → ["

我理解concat和slice方法,但我似乎不能理解这个例子,请帮助

function remove(array, index) {
  return array.slice(0, index)
    .concat(array.slice(index + 1));
}
console.log(remove(["a", "b", "c", "d", "e"], 2));
// → ["a", "b", "d", "e"]

摘自:Marijn Haverbeke。“雄辩的JavaScript.”Apple Books.

当试图理解一个复杂的链接在一起的函数调用字符串时,将它们分成单独的行并逐个评估它们所做的事情可能会有所帮助。Javascript进程调用从右到左:

    array.slice(index + 1) // ["d", "e"]
    .concat() // join the previous call's array with the next call
    array.slice(0, index) //  ["a", "b"]
    result ["a","b","d","e"]

我希望这有助于解释这些调用以及发生了什么。

您正在分割第一个项目和最后一个项目,然后将它们合并到一个数组中。中间的一个项目,不是其中任一个,都被删除了。那么代码的哪些部分你明白了?你到底被卡住了哪一部分?@MichaelBianconi最后一项应该是index+1之后的项,我想说的是concatenated not merged,这是一个集合操作。
array.slice(0, index) // ['a','b']
array.slice(index + 1) // ['d','e']

//then

.concat //they are getting concatinated

[ 'a', 'b', 'd', 'e' ]