一、.clear() →Array
function clear() {
this.length = 0;
return this;
}
例子:
var fruits = ['Apple', 'Orange', 'Bananas', 'peach']; fruits.clear(); // -> [] fruits // -> []
清除。
二、.clone() →Array
function clone() {
return slice.call(this, 0);
}
返回数组的副本,保持原始数组不变。
三、.compact() →Array
function compact() {
return this.select(function(value) {
return value != null;
});
}
返回一个副本,不包含null和undefined
例子:
var orig = [undefined, 'A', undefined, 'B', null, 'C']; var copy = orig.compact(); // orig -> [undefined, 'A', undefined, 'B', null, 'C']; // copy -> ['A', 'B', 'C'];
四、Every([iterator = Prototype.K[, context]]) → Boolean
-
iterator(Function) - 一个可选函数,用于评估枚举中的每个元素; 该函数应该返回值来测试。如果没有提供,则测试元素本身。 -
context(Object) -this在对迭代器的调用中使用的可选对象。
确定是否所有元素都是真实的(boolean-equivalent true),直接或通过提供的迭代器计算。(这一块我还不能很好理解)
function every(iterator) { if (this == null) throw new TypeError(); iterator = iterator || Prototype.K; var context = arguments[1]; var object = Object(this); for (var i = 0, length = object.length >>> 0; i < length; i++) { if (i in object && !iterator.call(context, object[i], i, object)) { return false; } } return true; } if (arrayProto.every) { every = wrapNative(Array.prototype.every); }
五、.filter(iterator[, context]) →Array
-
iterator(Function) - 用于测试元素的迭代器函数。 -
context(Object) -this在对迭代器的调用中使用的可选对象。
返回包含此数组中所有项目的新数组,其中 iterator返回了一个真值。
function filter(iterator) {
if (this == null || !Object.isFunction(iterator))
throw new TypeError();
var object = Object(this);
var results = [], context = arguments[1], value;
for (var i = 0, length = object.length >>> 0; i < length; i++) {
if (i in object) {
value = object[i];
if (iterator.call(context, value, i, object)) {
results.push(value);
}
}
}
return results;
}
if (arrayProto.filter) {
// `Array#filter` requires an iterator by nature, so we don't need to
// wrap it.
filter = Array.prototype.filter;
}
六、.first()
function first() { return this[0]; }
返回数组的第一个项目(例如,array[0])。
七、.flatten() →Array
function flatten() { return this.inject([], function(array, value) { if (Object.isArray(value)) return array.concat(value.flatten()); array.push(value); return array; }); }
个人理解:合并指定数组内的所有数组。
官方理解:
返回数组的平坦(一维)副本,保持原始数组不变。嵌套数组以递归方式内联注入。
常用在处理递归收集算法的结果。
例子:
var a = [ ' frank ',[ ' bob ',' lisa ' ],[ ' jill ',[ ' tom ',' sally ' ]]]; var b = a.flatten(); // a - > ['frank',['bob','lisa'],['jill',['tom','sally']]] // b - > ['frank','bob', 'lisa','jill','tom','sally']
八、.indexOf(item[, offser = 0]) →Number
function indexOf(item, i) {
if (this == null) throw new TypeError();
var array = Object(this), length = array.length >>> 0;
if (length === 0) return -1;
// The rules for the `fromIndex` argument are tricky. Let's follow the
// spec line-by-line.
i = Number(i);
if (isNaN(i)) {
i = 0;
} else if (i !== 0 && isFinite(i)) {
// Equivalent to ES5's `ToInteger` operation.
i = (i > 0 ? 1 : -1) * Math.floor(Math.abs(i));
}
// If the search index is greater than the length of the array,
// return -1.
if (i > length) return -1;
// If the search index is negative, take its absolute value, subtract it
// from the length, and make that the new search index. If it's still
// negative, make it 0.
var k = i >= 0 ? i : Math.max(length - Math.abs(i), 0);
for (; k < length; k++)
if (k in array && array[k] === item) return k;
return -1;
}
官方理解:
-
item(?) - 可能存在或不存在于数组中的值。 -
offset(Number) - 开始搜索前要跳过的初始项目数。
返回item数组中第一次出现的索引,或者-1如果item不存在于数组中。Array#indexOf使用绝对等于(===)比较项目。
个人理解:返回item在数组中首次出现的位置的索引,因为区分了使用了绝对等于所以区分大小写,类型。
例子:
[3, 5, 6, 1, 20].indexOf(1) // -> 3 [3, 5, 6, 1, 20].indexOf(90) // -> -1 (not found) ['1', '2', '3'].indexOf(1); // -> -1 (not found, 1 !== '1')
加一个不是Array的例子:
var str="Hello world!" document.write(str.indexOf("Hello") + "<br />") document.write(str.indexOf("World") + "<br />") document.write(str.indexOf("world")) // ->0 // ->-1 // ->6
九、.inspect() →String
function inspect() { return '[' + this.map(Object.inspect).join(', ') + ']'; }
返回数组的面向调试的字符串表示形式。
例子:
['Apples', {good: 'yes', bad: 'no'}, 3, 34].inspect()
// -> "['Apples', [object Object], 3, 34]"
十、.intersect(array) →Array
function intersect(array) {
return this.uniq().findAll(function(item) {
return array.indexOf(item) !== -1;
});
}
返回包含在两个给定数组之间相同的每个项目的数组。