This method will not change the existing array, but will only return a copy of the connected array.
For example:
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>
Output result:
1,2,3,4,5,11,22,33
Put all elements of the array into a string. Elements are separated by the specified delimiter.
For example:
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 result:
'
This is by far the fastest method! Using native code (such as join()), regardless of what the system does internally, is usually much faster than non-native. ——James Padolsey, james.padolsey.com
The pop() method will delete the last element of the array An element, decrements the array length by 1, and returns the value of the element it deletes.
If the array is already empty, pop() does not change the array and returns an undefined value
For example:
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>
Output result:
George,John,Thomas Thomas George,John
For example:
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>
Output result:
George,John,Thomas 4 George,John,Thomas,James
For example:
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>
Output result:
George,John,Thomas 4 James,George,John,Thomas
For example:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.reverse()); 5 </script>
Output result:
George,John,Thomas Thomas,John,George
For example:
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>
Output result:
George,John,Thomas George John,Thomas
Please note that this method does not modify the array, but returns a subarray
For example:
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>
Output result:
George,John,Thomas John,Thomas George,John,Thomas
Reference to the array. Please note that the array is sorted on the original array and no copy is generated
This method defaults to sorting in the order of character encoding (ASCII)
For example:
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>
Output Result:
John,George,Thomas George,John,Thomas
Let’s look at another example:
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>
Output result:
10,5,40,25,1000,1 1,10,1000,25,40,5
We can see that it is not sorted by numerical size as we think. If we want to sort by To sort by numerical size, you need to change the default sorting method and specify the sorting rules yourself.
As follows:
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>
Output result:
10,5,40,25,1000,1 1,5,10,25,40,1000
What if you want to sort in descending order?
Change the sorting rule to:
function (a, b) {return b – a;}
That’s OK
The splice() method has different functions from the slice() method. The splice() method will directly modify the array
(1) Delete the array elements in the specified range:
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>
Output result:
George,John,Thomas,James,Adrew,Martin George,John,Martin
(2) Insert the specified element starting from the specified subscript (the number of elements is not limited):
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>
Output result:
George,John,Thomas,James,Adrew,Martin George,John,William,JACK,Thomas,James,Adrew,Martin
(3) Delete the array elements in the specified range and replace them with the specified elements (the number of elements is not limited):
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>
Output result:
George,John,Thomas,James,Adrew,Martin George,John,William,JACK,Martin
The above is the detailed content of Detailed sample code explaining JavaScript array operation function methods. For more information, please follow other related articles on the PHP Chinese website!