JavaScript中数组的合并以及排序实现示例
作者:goldensun 时间:2024-05-08 10:10:01
合并两个数组 - concat()
源代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo">点击按钮合并数组。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction()
{
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);
var x=document.getElementById("demo");
x.innerHTML=children;
}
</script>
</body>
</html>
测试结果:
Cecilie,Lone,Emil,Tobias,Linus
合并三个数组 - concat()
源代码:
<!DOCTYPE html>
<html>
<body>
<script>
var parents = ["Jani", "Tove"];
var brothers = ["Stale", "Kai Jim", "Borge"];
var children = ["Cecilie", "Lone"];
var family = parents.concat(brothers, children);
document.write(family);
</script>
</body>
</html>
测试结果:
Jani,Tove,Stale,Kai Jim,Borge,Cecilie,Lone
数组排序(按字母顺序升序)- sort()
源代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to sort the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
测试结果:
Apple,Banana,Mango,Orange
数字排序(按数字顺序升序)- sort()
源代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to sort the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return a-b});
var x=document.getElementById("demo");
x.innerHTML=points;
}
</script>
</body>
</html>
测试结果:
1,5,10,25,40,100
数字排序(按数字顺序降序)- sort()
源代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to sort the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return b-a});
var x=document.getElementById("demo");
x.innerHTML=points;
}
</script>
</body>
</html>
测试结果:
100,40,25,10,5,1
将一个数组中的元素的顺序反转排序 - reverse()
源代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to reverse the order of the elements in the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction()
{
fruits.reverse();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
测试结果:
Mango,Apple,Orange,Banana
标签:JavaScript,数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用pandas按列合并两个文件的实例
2022-05-31 22:21:04
![](https://img.aspxhome.com/file/2023/3/107193_0s.jpg)
Golang 如何判断数组某个元素是否存在 (isset)
2024-02-04 17:11:46
Python模板的使用详细讲解
2022-03-20 13:24:08
![](https://img.aspxhome.com/file/2023/7/103237_0s.png)
python Popen 获取输出,等待运行完成示例
2022-06-22 20:43:58
深入浅析mybatis oracle BLOB类型字段保存与读取
2024-01-15 02:21:26
ASP UTF-8编码下字符串截取和获取长度函数
2011-03-30 10:52:00
Oracle存储过程之数据库中获取数据实例
2009-03-04 10:57:00
Python scrapy爬取小说代码案例详解
2021-09-18 17:03:52
![](https://img.aspxhome.com/file/2023/3/131213_0s.png)
ubuntu20.04配置mysql8.0的实现步骤
2024-01-28 14:56:16
![](https://img.aspxhome.com/file/2023/6/89936_0s.png)
Django中信号signals的简单使用方法
2023-08-18 08:49:49
浅析Python迭代器的高级用法
2022-12-21 16:57:03
PostgreSQL COALESCE使用方法代码解析
2024-01-28 19:55:01
使用python刷访问量的示例代码
2023-11-09 12:55:30
Scrapy模拟登录赶集网的实现代码
2023-02-22 20:33:47
![](https://img.aspxhome.com/file/2023/4/69474_0s.png)
python基于openpyxl生成excel文件
2022-08-03 03:10:47
![](https://img.aspxhome.com/file/2023/4/82684_0s.png)
python密码学库pynacl功能介绍
2021-03-07 01:29:44
Python安装spark的详细过程
2021-05-17 09:59:05
![](https://img.aspxhome.com/file/2023/1/109671_0s.png)
一文学会利用python解决文章付费限制问题
2021-04-09 08:23:51
![](https://img.aspxhome.com/file/2023/7/82537_0s.jpg)
Pandas:Series和DataFrame删除指定轴上数据的方法
2022-12-22 12:49:26
MySQL5.6下windows msi安装详细介绍
2024-01-14 04:42:50