js 数组常用方法
Array
array.concat(item...)
concat 方法返回一个新数组,它包含array的浅复制(shallow copy)并将1个或者多个参数item附加在其后。如果参数item是一个数组,那么它的每个元素会被分别添加。此外,参见后面的array.push(item...)方法。
|
var a = ['a', 'b', 'c'];
var b = ['x', 'y', 'z']; var c = a.concat(b, true); // c 是 ['a', 'b', 'c', 'x', 'y', 'z', true] |
array.join(separator)
join方法把一个array构造成一个字符串。它将array中的每个元素构造成一个字符串,并用一个separator为分隔符把它们连接在一起。默认的separator是','。为了实现无间隔的连接,我们可以使用空字符串作为separator。
如果想把大量的片段组装成一个字符串,把这些片段放到一个数组中并用join方法连接它们通常比用'+'运算符连接这些片段要快。
|
var a = ['a', 'b', 'c'];
a.push('d'); var c = a.join(''); // c 是 'abcd' |
array.pop()
pop和push方法使数组array像堆栈(stack)一样工作。pop方法移除array中的最后一个元素并返回该元素。如果该array是空的,它会返回undefined。
|
var a = ['a', 'b', 'c'];
var c = a.pop() // a 是 ['a', 'b'], c 是 'c' |
pop 可以像这样实现:
|
Array.method('pop', function(){
return this.splice(this.length - 1, 1)[0]; }); |
array.push(item...)
push方法将一个或多个参数item附加到一个数组的尾部。不像concat方法那样,它会修改该数组array(concat方法是返回一个新数组),如果参数item是一个数组,它会将参数数组作为单个元素整个添加到数组中。它返回这个数组array的新长度值。
|
var a = ['a', 'b', 'c'];
var b = ['x', 'y', 'z']; var c = a.push(b, true); // a 是 ['a', 'b', 'c', ['x', 'y', 'z'], true], c是5 |
push可以像这样实现:
|
Array.method('push', function () {
this.splice.apply( this, [this.length, 0].concat(Array.prototype.slice.apply(arguments))); return this.length; }); |
array.reverse()
reverse方法反转array中元素的顺序。返回当前的array:
|
var a = ['a', 'b', 'c'];
var b = a.reverse(); // a 和 b 都是 ['c', 'b', 'a'] |
array.shift()
shift方法移除数组array中的第一个元素并返回该元素。如果这个数组array是空的,它会返回undefined。shift通常比pop慢得多:
|
var a = ['a', 'b', 'c'];
var c = a.shift(); // a 是 ['b', 'c'], c是 'a' |
shift可以这样实现:
|
Array.method('shift', function(){
return this.splice(0, 1)[0]; }); |
array.slice(start, end)
slice方法对于array中的一段做浅复制。第一个被复制的元素是array[start]。他将一直复制到array[end]为止。end参数是可选的,并且默认值是该数组的长度array.length。如果两个参数中的任何一个数负数,array.length将和它们相加来试图使它们成为非负数。如果start大于等于array.length,得到的结果将是一个新的空数组。千万别把slice和splice混淆了。
|
var a = ['a', 'b', 'c'];
var b = a.slice(0, 1); // b是 ['a'] var c = a.slice(1); // c是 ['b', 'c'] var d = a.slice(1, 2); // d是 ['b'] |
array.sort(comparefn)
sort方法对array中的内容进行适当的排序。它不能正确地给一组数字排序:
|
var n = [4, 8, 15, 16, 23, 42];
n.sort(); // n是 [15, 16, 23, 4, 42, 8] |
js的默认比较函数假定所有要被排序的元素都是字符串。它尚未足够智能到在比较这些元素之前先检测它们的类型,所以当它比较这些数字的时候会将它们转为字符串,导致出错误的结果。
幸运的是,你可以使用自己的比较函数来替换默认的比较函数。你的比较函数应该接受两个参数,如果这两个参数相等则返回0;如果第一个参数应该排列在前面,则返回一个负数;如果第二个参数应该排列在前面,则返回一个正数。
|
var n = [4, 8, 15, 16, 23, 42];
n.sort(function(a, b){ return a - b; }); // n是 [4, 8, 15, 16, 23, 42] |
上面这个函数将给数字排序,但它不能给字符串排序。如果我们想要给任何简单值数组排序,则必须做更多的工作:
|
var m = ['aa', 'bb', 'a', 4, 8, 15, 16, 23, 42];
m.sort(function () { if (a === b) { return 0; } if (typeof a === typeof b) { return a < b ? -1 : 1; } return typeof a < typeof b ? -1 : 1; }); // m是 [4, 8 ,15, 16, 23, 42, 'a', 'aa', 'bb'] |
如果有一个更智能的比较函数,我们也可以给对象数组排序。为了在一般情况下让这个事情更容易,我们将编写一个构造比较函数的函数:
|
/**
* by 函数接受一个成员名字符串作为参数 * 并返回一个可以用来对包含该成员的对象数组进行排序的比较函数 */ var by = function(name){ return function(o, p){ var a, b; if(typeof o === 'object' && typeof p === 'object' && o && p){ a = o[name]; b = p[name]; if(a === b){ return 0; } if(typeof a === typeof b){ return a < b ? -1 : 1; } return typeof a < typeof b ? -1 : 1; }else{ throw { name: 'Error', message: 'Expected an object when sorting by' + name }; } } } var s = [ { first: 'Joe', last: 'Besser'}, { first: 'Moe', last: 'Howard'}, { first: 'Joe', last: 'DeRita'}, { first: 'Shemp', last: 'Howard'}, { first: 'Larry', last: 'Fine'}, { first: 'Curly', last: 'Howard'} ] s.sort(by('first')); /** * s是 * [ * { first: 'Curly', last: 'Howard'}, * { first: 'Joe', last: 'DeRita'}, * { first: 'Joe', last: 'Besser'}, * { first: 'Larry', last: 'Fine'}, * { first: 'Moe', last: 'Howard'}, * { first: 'Shemp', last: 'Howard'} * ] */ |
sort方法是不稳定的,所以下面的调用:
|
s.sort(by('first')).sort(by('last'));
|
不能保证产生正确的序列。如果想基于多个键值进行排序,需要更多的工作,修改by函数,让其可以接受第二个参数,当主要的键值产生一个匹配的时候,另一个compare方法将被调用以决出高下。
|
s.sort(by('first')).sort(by('last'));
var by = function(name, minor){ return function(o, p){ var a, b; if(typeof o === 'object' && typeof p === 'object' && o && p){ a = o[name]; b = p[name]; if(a === b){ return typeof minor === 'function' ? minor(o, p) : 0; } if(typeof a === typeof b){ return a < b ? -1 : 1; } return typeof a < typeof b ? -1 : 1; }else{ throw { name: 'Error', message: 'Expected an object when sorting by' + name }; } } } var s = [ { first: 'Joe', last: 'Besser'}, { first: 'Moe', last: 'Howard'}, { first: 'Joe', last: 'DeRita'}, { first: 'Shemp', last: 'Howard'}, { first: 'Larry', last: 'Fine'}, { first: 'Curly', last: 'Howard'} ] s.sort(by('first')); /** * s是 * [ * { first: 'Joe', last: 'Besser'}, * { first: 'Joe', last: 'DeRita'}, * { first: 'Larry', last: 'Fine'}, * { first: 'Curly', last: 'Howard'}, * { first: 'Moe', last: 'Howard'}, * { first: 'Shemp', last: 'Howard'} * ] */ |
array.splice(start, deleteCount, item...)
splice方法从array中移除1个或多个元素,并用心的item替换它们。参数start是从数组array中移除元素的开始位置。参数deleteCount是要移除的元素个数。如果有额外的参数,那么item都将插入到所移除元素的位置上。它返回一个包含被移除元素的数组。
splice最主要的用处是从一个数组中删除元素。钱外不要把splice和slice混淆了:
|
var a = ['a', 'b', 'c'];
var r = a.splice(1, 1, 'ache', 'bug'); // a是 ['a', 'ache', 'bug', 'c'], r是['b'] |
splice可以像这样实现:
|
Array.method('splice', function(start, deleteCount){
var max = Math.max, min = Math.min, delta, element, insetCount = max(arguments.length - 2, 0), k = 0, len = this.length, new_len, result = [], shift_count; start = start || 0; if(start < 0){ start += len; } start = max(min(start, len), 0); deleteCount = max(min(typeof deleteCount === 'number' ? deleteCount : len, len - start), 0); delta = insetCount - deleteCount; new_len = len + delta; while(k < deleteCount){ element = this[start + k]; if(element !== undefined){ result[k] = element; } k += 1; } shift_count = len - start - deleteCount; if(delta < 0){ k = start + insetCount; while(shift_count){ this[k] = this[k - delta]; k += 1; shift_count -= 1; } this.length = new_len }else if(delta > 0){ k = 1; while(shift_count){ this[new_len - k] = this[len - k]; k += 1; shift_count -= 1; } } for(k = 0; k < insetCount; k += 1){ this[start + k] = arguments[k + 2]; } return result; }); |
array.unshift(item...)
unshift方法像push方法一样用于将元素添加到数组中,但它是把item插入到array的开始部分而不是尾部。它返回array的新的长度值:
|
var a = ['a', 'b', 'c'];
var r = a.unshift('?', '@'); // a是 ['?', '@', 'a', 'b', 'c'], r是 5 |
IE6之前的浏览器中,JScript引擎对unshift方法的实现有误,返回值永远是undefined。IE7之后的浏览器修正了。
unshift可以像这样实现:
|
Array.method('unshift', function(){
this.splice.apply(this, [0, 0].concat(Array.prototype.slice.apply(arguments))); return this.length; }); |
浙公网安备 33010602011771号