javascript学习-Array
Array 数组对象
1.定义
var array = new Array();
var array = new Array(size);//size-数组大小
var array= new Array(element0,…,elementN);//element0,…,elementN放到数组中的元素
[ ] 符号访问数组中单个元素
2.属性
length
3.方法
- concat
array1.concat([item1[, item2[, . . . [, itemN]]]])
返回一个新数组,这个新数组是由两个或更多数组组合而成的。
- join
arrayObj.join(separator)
返回字符串值,其中包含了连接到一起的数组的所有元素,元素由指定的分隔符分隔开来
- pop
arrayObj.pop( )
移除数组中的最后一个元素并返回该元素
- push
arrayObj.push([item1 [item2 [. . . [itemN ]]]])
将新元素添加到一个数组中,并返回数组的新长度值
- reverse
arrayObj.reverse( )
返回一个元素顺序被反转的 Array 对象
- shift
arrayObj.shift( )
移除数组中的第一个元素并返回该元素
- unshift
arrayObj.unshift([item1[, item2 [, . . . [, itemN]]]])
将指定的元素插入数组开始位置并返回该数组
- slice
arrayObj.slice(start, [end])
返回一个数组的一段
- sort
arrayobj.sort(sortfunction)
返回一个元素已经进行了排序的 Array 对象,默认按照 ASCII 字符顺序进行升序排列
- splice
arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
从一个数组中移除一个或多个元素,如果必要,在所移除元素的位置上插入新元素,返回所移除的元素
- toString
将 Array 的元素转换为字符串。结果字符串由逗号分隔,且连接起来
- valueOf
将 Array 的元素转换为字符串。结果字符串由逗号分隔,且连接起来
4.扩展方法
引用自:http://erik.eae.net/archives/2005/06/05/17.53.19/
// Mozilla 1.8 has support for indexOf, lastIndexOf, forEach, filter, map, some, every
// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:lastIndexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (obj, fromIndex) {
if (fromIndex == null) {
fromIndex = 0;
} else if (fromIndex < 0) {
fromIndex = Math.max(0, this.length + fromIndex);
}
for (var i = fromIndex; i < this.length; i++) {
if (this[i] === obj)
return i;
}
return -1;
};
}
// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:lastIndexOf
if (!Array.prototype.lastIndexOf) {
Array.prototype.lastIndexOf = function (obj, fromIndex) {
if (fromIndex == null) {
fromIndex = this.length - 1;
} else if (fromIndex < 0) {
fromIndex = Math.max(0, this.length + fromIndex);
}
for (var i = fromIndex; i >= 0; i--) {
if (this[i] === obj)
return i;
}
return -1;
};
}
// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (f, obj) {
var l = this.length; // must be fixed during loop... see docs
for (var i = 0; i < l; i++) {
f.call(obj, this[i], i, this);
}
};
}
// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
if (!Array.prototype.filter) {
Array.prototype.filter = function (f, obj) {
var l = this.length; // must be fixed during loop... see docs
var res = [];
for (var i = 0; i < l; i++) {
if (f.call(obj, this[i], i, this)) {
res.push(this[i]);
}
}
return res;
};
}
// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:map
if (!Array.prototype.map) {
Array.prototype.map = function (f, obj) {
var l = this.length; // must be fixed during loop... see docs
var res = [];
for (var i = 0; i < l; i++) {
res.push(f.call(obj, this[i], i, this));
}
return res;
};
}
// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:some
if (!Array.prototype.some) {
Array.prototype.some = function (f, obj) {
var l = this.length; // must be fixed during loop... see docs
for (var i = 0; i < l; i++) {
if (f.call(obj, this[i], i, this)) {
return true;
}
}
return false;
};
}
// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:every
if (!Array.prototype.every) {
Array.prototype.every = function (f, obj) {
var l = this.length; // must be fixed during loop... see docs
for (var i = 0; i < l; i++) {
if (!f.call(obj, this[i], i, this)) {
return false;
}
}
return true;
};
}
Array.prototype.contains = function (obj) {
return this.indexOf(obj) != -1;
};
Array.prototype.copy = function (obj) {
return this.concat();
};
Array.prototype.insertAt = function (obj, i) {
this.splice(i, 0, obj);
};
Array.prototype.insertBefore = function (obj, obj2) {
var i = this.indexOf(obj2);
if (i == -1)
this.push(obj);
else
this.splice(i, 0, obj);
};
Array.prototype.removeAt = function (i) {
this.splice(i, 1);
};
Array.prototype.remove = function (obj) {
var i = this.indexOf(obj);
if (i != -1)
this.splice(i, 1);
};