代码之家  ›  专栏  ›  技术社区  ›  batman091716

如何使此函数按预期工作?

  •  1
  • batman091716  · 技术社区  · 6 年前

    我试图创建一个可以复制现有数组的函数 sunsetColors ,并将第一个元素更改为“蓝色”。

    然后我的 sunset 函数应返回数组的副本。请帮助我理解我需要改变什么才能使它按预期工作。

    var sunsetColors = ["brown", "purple", "pink", "red", "orange", "yellow"]
    

    我要尝试的是:

    sunset = () => {
      return sunsetColors.splice(0, 1, 'blue');
    }
    
    4 回复  |  直到 6 年前
        1
  •  4
  •   Travis J    6 年前

    拼接将返回

    包含已删除元素的数组。如果只删除一个元素,则返回一个元素数组。如果没有删除任何元素,则返回空数组。 MDN - splice

    所以您需要返回集合,而不是拼接的结果

    var sunsetColors = ["brown", "purple", "pink", "red", "orange", "yellow"];
    sunset = () => {
        sunsetColors.splice(0, 1, 'blue');
        return sunsetColors;
    };
    console.log(sunset());

    这将使您的函数“按预期”工作(因为它现在正在返回修改后的数组)。但是,“splice()方法通过删除现有元素和/或添加新元素来更改数组的内容。”。如果你只打算修改一个副本,那么你应该先制作一个副本,然后再修改那个副本。

    使用Slice和Unshift是一种很好的方法, @mahan alludes to this in their answer 也。

    "The slice() method returns a shallow copy of a portion of an array" ,包括复制方面,以及 "the unshift() method adds one or more elements to the beginning of an array" ,这将覆盖插入。

    它看起来像这样,仍然使用箭头函数:

    var sunsetColors = ["brown", "purple", "pink", "red", "orange", "yellow"];
    sunset = () => {
        let arrayCopy = sunsetColors.slice(1);
        arrayCopy.unshift('blue');
        return arrayCopy;
    };
    console.log(sunset());
        2
  •  1
  •   mahan    6 年前

    使用 .slice() 方法来克隆数组。

    var sunsetColors = ["brown", "purple", "pink", "red", "orange", "yellow"]
    
    function copySunsetColor(list, color) {
      var colorList = list.slice();
      // Add 'color' at the start of the list 
      colorList[0] = color
      return colorList;
    }
    
    console.log(copySunsetColor(sunsetColors, "blue"))

    使用 unshift 在列表的开头添加项。

    var sunsetColors = ["brown", "purple", "pink", "red", "orange", "yellow"]
    
    function copySunsetColor(list, color) {
      var colorList = list.slice();
      // Replace the first item of the list with 'color' input
      colorList.unshift(color)
      return colorList;
    }
    
    console.log(copySunsetColor(sunsetColors, "blue"))
        3
  •  0
  •   szczepaniakdominik    6 年前

    试试这个解决方案。首先,它创建一个数组的副本(不是引用),然后替换第一个元素。

    let sunsetColors = ["brown", "purple", "pink", "red", "orange", "yellow"];
    
    copy = (array) => {
      let newArray = [...array];
      newArray[0] = 'blue';
      return newArray;
    };
        4
  •  0
  •   Luis felipe De jesus Munoz    6 年前

    可以将数组添加到包含 blue .

    sunset = ['blue'].concat(sunsetColors)

    var sunsetColors = ["brown", "purple", "pink", "red", "orange", "yellow"]
    
    sunset = ['blue'].concat(sunsetColors.slice(1))
    
    console.log(sunset)

    如果需要更改与第一个元素不同的另一个值,可以使用如下函数:

    f = (array, position, value) => { array[position] = value; return array }

    var sunsetColors = ["brown", "purple", "pink", "red", "orange", "yellow"]
    
    f = (array, position, value) => { array[position] = value; return array }
    console.log(f(sunsetColors, 0, 'blue'))