如何在不改变原始数组的情况下拼接数组?

王林
王林 转载
2023-08-25 09:49:02 897浏览

如何在不改变原始数组的情况下拼接数组?

在 JavaScript 中,我们可以使用 splice() 方法来拼接数组。 splice() 方法在数组中插入或删除单个或多个元素。我们可以将起始索引作为 splice() 方法的第一个参数传递,从中我们可以插入或删除元素。它将要从数组中删除的元素数量作为第二个参数,将数组值作为要插入到数组中的第三个参数。

本教程将教我们在不改变原始数组的情况下拼接数组。 改变原始数组意味着对数组进行更改。每当我们将原始数组与 splice() 方法一起使用时,它都会从原始数组中插入或删除一些元素。因此,我们将克隆该数组,并对克隆数组使用 splice() 方法来保持原始数组相同。

语法

用户可以按照下面的语法来使用 array.splice() 方法。

  let results = array.splice(startIndex, count, element1, element2, element3, ... , elementN);

参数

  • startIndex - 这是从数组中插入或删除元素的第一个索引。

  • Count - 它是数组中要替换的元素数量。如果我们传递 0 作为 count 值,它将在 startIndex 处插入元素。

  • Element1, element2, …, element - 这是新的数组元素,从起始索引开始替换或插入。

现在,我们将看到在不改变原始数组的情况下拼接数组的不同方法。

使用扩展运算符拼接数组而不改变原始数组

扩展运算符允许我们克隆数组。我们可以使用展开运算符创建原始数组的克隆,之后,我们可以使用splice()方法将克隆数组用于splice 不改变原始数组的数组。

语法

用户可以按照下面的语法使用扩展运算符来拼接数组,而不会改变原始数组。

let splicedArray = [...array].splice(0, 3);

在上面的语法中,数组是一个原始数组,我们使用展开运算符对其进行克隆,并对克隆的数组使用 splice() 方法。

示例 1

在下面的示例中,我们创建了一个包含各种字符串的数组。之后,我们使用带有“[]”大括号中的扩展运算符的数组来克隆它,并使用 splice() 方法。

我们在 splice() 方法中传递了 0 作为起始索引,并传递了 3 作为要从数组中删除的元素总数。

<html>
   <body>
      <h3> Using the <i> spread operator </i> to splice an array without mutating the original array. </h3>
      <div id = "output"> </div>
      <script>
         let output = document.getElementById('output');
         let array = ["C", "Java", "CPP", "JavaScript", "TypeScript", "html", "HTML", "NodeJs", "ReactJs", "NextJs"];
         let splicedArray = [...array].splice(0, 3);
         output.innerHTML += "Orignal array after the splicing: " + array + "<br>";
         output.innerHTML += "Spliced array after the splicing:" + splicedArray + "<br>";
      </script>
   </body>
</html>

使用filter()方法拼接数组而不改变原始数组

我们可以通过过滤原始数组中的元素来创建一个新数组。 splice() 方法从起始索引中提取总数的元素。因此,我们可以使用 filter() 方法从起始索引中过滤掉总计数元素。

语法

用户可以按照下面的语法使用filter()方法来拼接数组,而不会改变原始数组。

let splicedArray = array.filter((ele, ind) => {
   if (ind >= startIndex && ind < counts + startIndex) {
      return true;
   }
      return false;
})

在上面的语法中,我们过滤了从 startIndex 到 count + startIndex 的数组元素。

示例 2

在下面的示例中,我们有一个数组并对该数组使用 filter() 方法。用户可以看到 filter() 方法如何从 startIndex 中提取元素总数。

此外,当我们对原始数组使用 filter() 方法时,它保持不变。

<html>
   <body>
      <h3> Using the <i> filter() method </i> to splice an array without mutating the original array. </h3>
      <div id = "output"> </div>
      <button onclick = "spliceArray()"> Splice an array </button>
      <script>
         let output = document.getElementById('output');
            function spliceArray() {
               let startIndex = 5;
               let counts = 3;
               let array = ["C", "Java", "Cpp", "JavaScript", "TypeScript", "html", "HTML", "NodeJs", "ReactJs", "NextJs"];
            
            let splicedArray = array.filter((ele, ind) => {
               if (ind >= startIndex && ind < counts + startIndex) {
                  return true;
               }
                  return false;
            })
            output.innerHTML += "Orignal array after the splicing is " + array + "<br>";
            output.innerHTML += "Spliced array after the splicing is " + splicedArray + "<br>";
         }
      </script>
   </body>
</html>

使用 slice() 方法拼接数组,而不改变原始数组

slice()方法提取数组元素,这里我们将使用数组的副本。当我们将 0 作为 slice() 方法的第一个参数传递,同时将其与数组一起使用时,它会克隆该数组。

之后,我们可以使用splice()方法来拼接数组,而无需改变原始数组。

语法

用户可以按照下面的语法使用slice()splice()方法来拼接数组,而不会改变原始数组。

let clonedArray = array.slice(0);
let splicedArray = clonedArray.splice(start, end);

在上面的语法中,首先,我们使用 slice() 方法来克隆数组,然后使用 splice() 方法来 splice() 一个数组。

示例 3

下面的示例包含具有多个数值的数字数组。之后,clonedArray包含原始数组的克隆。

接下来,我们将splice()方法与clonedArray一起使用,用户可以检查原始数组是否相同。

<html>
   <body>
      <h3> Using the <i> slice() </i> method to splice an array without mutating the original array </h3>
      <div id = "output"> </div>
      <script>
         let output = document.getElementById('output');
         let array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
         let clonedArray = array.slice(0);
         let splicedArray = clonedArray.splice(1, 6);
         output.innerHTML += "Orignal array after the splicing: " + array + "<br>";
         output.innerHTML += "Spliced array after the splicing: " + splicedArray + "<br>";
      </script>
   </body>
</html>

以上就是如何在不改变原始数组的情况下拼接数组?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除