1.length;获取数组的长度;
<script type="text/javascript">
var txt="Hello World!"
document.write(txt.length)
</script>
输出:
12
2.push();将值添加到数组的尾部;
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.push("James") + "<br />")
document.write(arr)
</script>
输出:
George,John,Thomas
4
George,John,Thomas,James
3.unshift();将值添加数组的头部;
<script type="text/javascript">
var arr = new Array()
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.unshift("William") + "<br />")
document.write(arr)
</script>
输出:
George,John,Thomas
4
William,George,John,Thomas
4.pop();删除数组的最后一个元素,并且返回删除元素;
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();
fruits 结果输出:
Banana,Orange,Apple
5.splice();从一个数组当中删除一个或多个元素,返回所移除的元素;
<script type="text/javascript">
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
document.write(arr + "<br />")
arr.splice(2,0,"William")
document.write(arr + "<br />")
</script>
输出:
George,John,Thomas,James,Adrew,Martin
George,John,William,Thomas,James,Adrew,Martin
6.slice(): 方法可从已有的数组中返回选定的元素.
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.slice(1) + "<br />")
document.write(arr)
</script>
输出:
George,John,Thomas
John,Thomas
George,John,Thomas
7.concat(arr1,arr2);将多个数组合并为一个数组,并且返回合并数组;
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
var arr2 = new Array(3)
arr2[0] = "James"
arr2[1] = "Adrew"
arr2[2] = "Martin"
var arr3 = new Array(2)
arr3[0] = "William"
arr3[1] = "Franklin"
document.write(arr.concat(arr2,arr3))//返回结果George,John,Thomas,James,Adrew,Martin,William,Franklin
8.join(“分隔符”);将数组转换为字符串,字符串按照分隔符分割,返回分割好的字符串
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.join())
</script>
输出:
George,John,Thomas
9.reverse();反转数组,返回翻转后的数组;
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.reverse())
</script>
输出:
George,John,Thomas
Thomas,John,George
10.concat():基于当前数组,创建一个新的数组,并返回这个新数组,不会改变原数组;
var color = ["red","green","blue"];
var colors2 = color.concat("yellow",["black","brown"]);
11.shift():方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.shift() + "<br />")
document.write(arr)
</script>
输出:
George,John,Thomas
George
John,Thomas
12.String() :对象用于处理文本(字符串)。
new String(s);
String(s);