freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../functional-programming/add-elements-to-the-end-of-.../index.md

851 B
Raw Blame History

title localeTitle
Add Elements to the End of an Array Using concat Instead of push 使用concat将元素添加到数组的末尾而不是push

使用concat将元素添加到数组的末尾而不是push

push方法将新元素添加到原始数组的末尾的地方, concat方法创建一个新数组,其中包含原始数组和新元素中的元素。使用concat时,原始数组保持不变。

相关链接:

function nonMutatingPush(original, newItem) { 
 
  // Add your code below this line 
 
  return original.concat(newItem); 
 
  // Add your code above this line 
 } 
 
 var first = [1, 2, 3]; 
 var second = [4, 5]; 
 nonMutatingPush(first, second);