使用 jQuery/JavaScript 合并数组
在本教程中,我们将学习如何使用 JavaScript 和 JQuery 合并给定的数组。在 Web 开发中,有很多情况需要合并数组。例如,我们给定一个标签列表,需要将其合并成单个字符串以插入网页。另一个可能需要合并数组的情况是在编写 SQL 查询时。
这里,我们将学习四种连接给定数组元素的方法。
使用 Array.join() 方法合并数组
array.join() 方法允许我们通过使用特定分隔符分隔所有元素来将数组元素连接成单个字符串。
语法
用户可以按照以下语法使用 JavaScript 的 join() 方法合并数组。
Array.join(delimiter)
在上述语法中,“Array” 是要合并的参考数组,“delimiter” 是我们需要用来连接数组元素的字符。
示例
在下面的示例中,我们创建了包含水果名称的“arr”数组。之后,我们使用 array.join() 方法连接所有水果名称并将它们存储在“fruits”字符串中。
在输出中,我们可以看到包含数组中所有水果名称的“fruits”字符串。
<html>
<body>
<h3> Using the <i> array.join() </i> method to implode the array in JavaScript </h3>
<div id="output"> </div>
<script>
let output = document.getElementById('output');
// Array of fruit names
let arr = ["Banana", "Orange", "Apple", "Mango"];
// Join the array elements
let fruits = arr.join();
output.innerHTML = "The original array is: " + arr + "<br><br>";
output.innerHTML += "The joined array is: " + fruits;
</script>
</body>
</html>
示例
在下面的示例中,我们创建了包含颜色名称的数组。之后,我们使用了 join() 方法并将“|”字符作为 join() 方法参数传递,以分隔符分隔每个数组元素。
在输出中,用户可以看到原始数组元素和合并后数组的结果。
<html>
<body>
<h3> Using the <i> array.join() </i> method to implode the array in JavaScript </h3>
<div id="output"> </div>
<script>
let output = document.getElementById('output');
let colors = ["Red", "Green", "White", "Black"];
// Join the array elements
let colorStr = colors.join(' | ');
output.innerHTML = "The original array is: " + colors + "<br><br>";
output.innerHTML += "The joined array is: " + colorStr;
</script>
</body>
</html>
使用 For 循环和“+”运算符在 JavaScript 中合并数组
我们可以使用 for 循环或 while 循环来遍历数组。在遍历数组元素时,我们可以使用“+”或“+=”运算符连接它们。此外,我们可以在连接数组元素时使用分隔符。
语法
用户可以按照以下语法使用 for 循环和“+”运算符合并数组。
for ( ) {
result += array[i];
}
在上述语法中,我们将 arry[i] 附加到“result”字符串。
示例
在下面的示例中,我们创建了包含按递增顺序排列的数字的数组。我们创建了“numberStr”变量来存储合并后数组的结果。
我们使用 for 循环遍历数组,并在每次迭代中将 number[i] 附加到“numberStr”。此外,我们在将每个元素附加到“numberStr”字符串后添加“ < ”分隔符。
在输出中,我们可以看到我们通过合并数组准备了包含“ < ”和数组元素的字符串。
<html>
<body>
<h3>Using the <i> for loop and + operator </i> to implode the array in JavaScript</h3>
<div id="output"> </div>
<script>
let output = document.getElementById('output');
let number = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
let numberStr = "";
// Using the for loop
for (let i = 0; i < number.length; i++) {
numberStr += number[i];
if (i < number.length - 1) {
numberStr += " < ";
}
}
output.innerHTML = "The original array is: " + number + "<br><br>";
output.innerHTML += "The joined array is: " + numberStr;
</script>
</body>
</html>
使用 Array.reduce() 方法和“+”运算符在 JavaScript 中合并数组
array.reduce() 方法通过合并数组将数组列表转换为单个元素。在这里,我们将执行 array.reduce() 方法,以数组为参考,并将回调函数作为参数传递以连接数组元素。
语法
用户可以按照以下语法使用 array.reduce() 方法合并数组。
message.reduce((a, b) => a + " " + b);
在上述语法中,我们传递了回调函数来连接数组元素。
示例
在下面的示例中,我们创建了一个包含消息字符串的数组。之后,我们以“message”数组为参考,执行 reduce() 方法来合并数组。
此外,我们将 a 和 b 参数传递给 reduce() 方法的回调函数。每次迭代后,“a”存储合并后数组的结果,“b”表示当前数组元素。函数体通过空格分隔将“b”附加到“a”。
<html>
<body>
<h3>Using the <i> array.reduce() method </i> to implode the array in JavaScript </h3>
<div id="output"> </div>
<script>
let output = document.getElementById('output');
let message = ["Hello", "Programmer", "Welcome", "to", "JavaScript", "Tutorial"];
// Using the array.reduce() method
let messageStr = message.reduce((a, b) => a + " " + b);
output.innerHTML = "The original array is: " + message + "<br><br>";
output.innerHTML += "The joined array is: " + messageStr;
</script>
</body>
</html>
使用 Each() 方法在 JQuery 中合并数组
JQuery 的 Each() 方法用于遍历数组元素。我们可以遍历数组元素并逐个连接每个元素。
语法
用户可以按照以下语法使用 JQuery 的 each() 方法合并数组。
$.each(array, function (index, value) {
treeStr += value + " ";
});
在上述语法中,each() 方法将要合并的数组作为第一个参数,将连接数组的回调函数作为第二个参数。
示例
在下面的示例中,我们创建了包含树名称的数组。之后,我们使用了 JQuery 的 each() 方法来遍历数组。我们在 each() 方法的回调函数中获取当前索引和元素值。因此,我们将元素值附加到“treeStr”。
最后,我们可以看到输出中“treeStr”的值。
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
</head>
<body>
<h3>Using the <i> each() method </i> to implode the array in jQuery</h3>
<div id="output"> </div>
<script>
let output = document.getElementById('output');
let tree = ["oak", "pine", "ash", "maple", "walnut", "birch"];
let treeStr = "";
// Using the each() method
$.each(tree, function (index, value) {
treeStr += value + " ";
});
output.innerHTML = "The original array is: " + tree + "<br><br>";
output.innerHTML += "The joined array is: " + treeStr;
</script>
</body>
</html>
array.join() 方法是 JavaScript 中合并数组的最佳方法之一。但是,如果程序员在合并数组时需要更多自定义,也可以使用 for 循环和“+”运算符。在 JQuery 中,程序员可以使用 each() 方法或 makeArray() 和 join() 方法,其工作方式与 JavaScript 的 join() 方法类似。
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP