JavaScript, 如何替換陣列中的元素

如果你知道陣列中的某個元素的索引,你可以通過簡單的賦值來替換它的內容: const items = ['a', 'b', 'c', 'd', 'e', 'f'] const i = 2 items[i] = '--NEW-ITEM--' console.log(items) //[ 'a', 'b', '--NEW-ITEM--', 'd', 'e', 'f' ] 如果你不知道元素的索引,你可能需要首先找到元素在陣列中的索引。