一、数组方法
shift() 数组最前面删除元素
unshift() 数组最前面前添加元素
push() 数组最后面添加元素
pop() 数组最后面删除元素
删除某个数组元素 delete arr[2]
concat()数组合并 array.concat(b)
var arry= [1,2,3,4,5];
var b = [111,22,3];
var c = arry.concat(b);
console.log(c) // [1, 2, 3, 4, 5, 111, 22, 3]
splice() 找到索引位置,并删除,再在该位置添加新元素(0为不删除)改变原来数组
arrayObject.splice(index,howmany,item1,.....,itemX)
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,3,"William")
document.write(arr)
slice()选取元素
arrayObject.slice(start,end) 选取索引区间内的元素
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 />")
document.write(arr.slice(2,4) + "<br />")
document.write(arr)
// George,John,Thomas,James,Adrew,Martin
// Thomas,James
// George,John,Thomas,James,Adrew,Martin
reverse()将数组反序
sort()排序正序 ,可用于获取最小值和最大值
var a = [2,5,9,8,5,6]
var b = a.sort() // [2,5,9,8,5,6]
var max = b[0] // 2
var min = b[a.length-1] // 9
join(),split() 把数组转化为字符串
join() 将数组放到一个字符串里,并可以用‘.’等字符分隔(默认用逗号分隔)
var a = [1,2,3,4,5];
var b = a.join("|"); //a:[1,2,3,4,5] b:"1|2|3|4|5"
split() 把数组按某个字符切割 ,并返回数组
var s = "abc,abcd,aaa";
ss = s.split(",");// 在每个逗号(,)处进行分解 ["abc", "abcd", "aaa"]
var s1 = "helloworld";
ss1 = s1.split(''); //["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
toString() 把数组转化为字符串
localeString() 把数组转化为本地字符串
toSource() 返回对象的源代码
tip:在Array中检索出一个指定的值,我们需要遍历整个数组:
var keyword = 3;
var ary = [2,3,4]
for ( var i=0 ; i < ary.length ; ++i )
{
if ( ary[i] == keyword )
{
console.log(keyword)
}
}
二、数组属性
length
prototype
使你有能力为对象添加属性和方法
constructor
返回 对创建此对象的 函数原型
<script type="text/javascript">
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
document.write(bill.constructor);
</script>