如何在不修改原始数组的情况下拼接数组?
在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()`方法来拼接数组,而不会修改原始数组。
语法
用户可以按照以下语法使用扩展运算符在不修改原始数组的情况下拼接数组。
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()`方法拼接数组。
示例3
下面的示例包含一个包含多个数值的数字数组。之后,`clonedArray`包含原始数组的克隆。
接下来,我们使用`clonedArray`和`splice()`方法,用户可以检查原始数组是否保持不变。
<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>
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP