您好,欢迎访问一九零五行业门户网

详解JavaScript数组操作函数方法的示例代码

1、concat() 连接两个或更多的数组该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。
例如:
1 <script type="text/javascript"> 2 var arr = [1, 2, 3]; 3 var arr1 = [11, 22, 33]; 4 document.write(arr.concat(4, 5, arr1)); 5 </script>
输出结果:
1,2,3,4,5,11,22,33
2、join()把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。
例如:
1 <script type="text/javascript"> 2 var arr = ['item 1', 'item 2', 'item 3']; 3 var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>'; 4 </script>
list结果:
‘<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>’
这是迄今为止最快的方法!使用原生代码(如 join()),不管系统内部做了什么,通常比非原生快很多。——james padolsey, james.padolsey.com
3、pop() 删除并返回数组的最后一个元素pop()方法将删除数组的最后一个元素,把数组长度减 1,并且返回它删除的元素的值。
如果数组已经为空,则pop()不改变数组,并返回undefined值
例如:
1 <script type="text/javascript"> 2 var arr = ["george", "john", "thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.pop() + "<br/>"); 5 document.write(arr); 6 </script>
输出结果:
george,john,thomas thomas george,john
4、push() 向数组的末尾添加一个或更多元素,并返回新的长度例如:
1 <script type="text/javascript"> 2 var arr = ["george", "john", "thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.push("james") + "<br/>"); 5 document.write(arr); 6 </script>
输出结果:
george,john,thomas 4 george,john,thomas,james
5、unshift() 向数组的开头添加一个或更多元素,并返回新的长度例如:
1 <script type="text/javascript"> 2 var arr = ["george", "john", "thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.unshift("james") + "<br/>"); 5 document.write(arr); 6 </script>
输出结果:
george,john,thomas 4 james,george,john,thomas
6、reverse() 颠倒数组中元素的顺序例如:
1 <script type="text/javascript"> 2 var arr = ["george", "john", "thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.reverse()); 5 </script>
输出结果:
george,john,thomas thomas,john,george
7、shift() 删除并返回数组的第一个元素例如:
1 <script type="text/javascript"> 2 var arr = ["george", "john", "thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.shift() + "<br/>"); 5 document.write(arr); 6 </script>
输出结果:
george,john,thomas george john,thomas
8、slice(start,end) 从某个已有的数组返回选定的元素请注意,该方法并不会修改数组,而是返回一个子数组
例如:
1 <script type="text/javascript"> 2 var arr = ["george", "john", "thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.slice(1) + "<br/>"); // 从第一个元素开始截取到 数组结尾 5 document.write(arr); 6 </script>
输出结果:
george,john,thomas john,thomas george,john,thomas
9、sort() 对数组的元素进行排序对数组的引用。请注意,数组在原数组上进行排序,不生成副本
该方法默认是按照字符编码(ascii)的顺序进行排序的
例如:
1 <script type="text/javascript"> 2 var arr = new array(6); 3 arr[0] = "john"; 4 arr[1] = "george"; 5 arr[2] = "thomas"; 6 document.write(arr + "<br/>"); 7 document.write(arr.sort()); 8 </script>
输出结果:
john,george,thomas george,john,thomas
再来看一个例子:
1 <script type="text/javascript"> 2 var arr = new array(6); 3 arr[0] = 10 4 arr[1] = 5 5 arr[2] = 40 6 arr[3] = 25 7 arr[4] = 1000 8 arr[5] = 1 9 document.write(arr + "<br/>"); 10 document.write(arr.sort()); 11 </script>
输出结果:
10,5,40,25,1000,1 1,10,1000,25,40,5
我们可以看到,并非是按照我们认为的按数字大小排序,如果想按照数字大小排序,则需要改变默认的排序方式,自行指定排序规则。
如下:
1 <script type="text/javascript"> 2 var arr = new array(6); 3 arr[0] = 10 4 arr[1] = 5 5 arr[2] = 40 6 arr[3] = 25 7 arr[4] = 1000 8 arr[5] = 1 9 document.write(arr + "<br/>"); 10 document.write(arr.sort(function (a, b) {return a - b;}));// 从大到小 11 </script>
输出结果:
10,5,40,25,1000,1 1,5,10,25,40,1000
如果想要降序排列呢?
将排序规则改为:
function (a, b) {return b – a;}
就ok了
10、splice() 删除元素,并向数组添加新元素splice() 方法与 slice() 方法的作用是不同的,splice() 方法会直接对数组进行修改
(1)删除指定范围的数组元素:
1 <script type="text/javascript"> 2 var arr = new array(6); 3 arr[0] = "george"; 4 arr[1] = "john"; 5 arr[2] = "thomas"; 6 arr[3] = "james"; 7 arr[4] = "adrew"; 8 arr[5] = "martin"; 9 10 document.write(arr + "<br/>"); 11 arr.splice(2, 3); // 删除第三个元素以后的三个数组元素(包含第三个元素) 12 document.write(arr); 13 </script>
输出结果:
george,john,thomas,james,adrew,martin george,john,martin
(2)从指定下标开始插入指定元素(元素个数不限):
1 <script type="text/javascript"> 2 var arr = new array(6); 3 arr[0] = "george"; 4 arr[1] = "john"; 5 arr[2] = "thomas"; 6 arr[3] = "james"; 7 arr[4] = "adrew"; 8 arr[5] = "martin"; 9 10 document.write(arr + "<br/>"); 11 arr.splice(2, 0, "william","jack"); // 在第三个元素之前插入"william","jack" 12 document.write(arr); 13 </script>
输出结果:
george,john,thomas,james,adrew,martin george,john,william,jack,thomas,james,adrew,martin
(3)删除指定范围的数组元素,并用指定元素替换(元素个数不限):
1 <script type="text/javascript"> 2 var arr = new array(6); 3 arr[0] = "george"; 4 arr[1] = "john"; 5 arr[2] = "thomas"; 6 arr[3] = "james"; 7 arr[4] = "adrew"; 8 arr[5] = "martin"; 9 10 document.write(arr + "<br/>"); 11 arr.splice(2,3,"william","jack"); // 删除第三个元素以后的三个数组元素(包含第三个元素),并用"william","jack"进行替换 12 document.write(arr); 13 </script>
输出结果:
george,john,thomas,james,adrew,martin george,john,william,jack,martin
以上就是详解javascript数组操作函数方法的示例代码的详细内容。
其它类似信息

推荐信息