1. 程式人生 > >js array methods

js array methods

js array method

array has some build-in method, to manipulate array,

------

join

join array elements and return a string, elements are separated by a separator,

the default separator is ',' , also you can specify one,

e.g.

	var arr = [1,2,3,4];
	console.log(arr.join());
	console.log(arr.join('-'));
 

------

reverse

reverse array elements, and return the reversed array, the method effect the original array,

e.g.:

	var arr = [1,2,3,4];
	console.log(arr);
	arr.reverse();
	console.log(arr);
 

------

sort

sort array, and effect the original array, 

default order:

default order is according to alphabetical order, (elements will be convert to string temporarily if necessary),

comparison function:

you can specify a comparison function as argument, which specify the order,

there are 2 param for the comparsion function, 

return a num:

<0means first param is appear first,

>0means second param is appear first,

=0means equal,

e.g.

	var arr = [5,1,7,2,9,3,4];
	console.log(arr);
	console.log(arr.sort());	// order from small to big
	console.log(arr.sort(function(a,b) { // order from big to small
	    return b-a;
	}));
 

------

concat

append element to array, and return the new array, the original array will not change,

there could be multiple params which might be single element or array, they will be add by order,

array param:

if param has array, it will be treat as a unit, and will not be separated,

e.g.

	var arr = [1,2,3];
	console.log(arr);
	console.log(arr.concat(4,5,[6,7],[[8,9],[[10,11],[12,13]]]));
	console.log(arr);
 

------

slice

slice(x,y), return sub array by index [x,y), and not effect the original array,

if second param is missing, then return to the end,

if param is <0, then +=length once, if still <0, then nothing will be returned,

if index out of range, then nothing will be returned,

if first>=second, then nothing will be returned,

e.g.

	var arr = [1,2,3,4,5];
	console.log(arr);
	console.log(arr.slice(1,3));
	console.log(arr.slice(1));
	console.log(arr.slice(-3,-1));
	console.log(arr.slice(-3,-20));
	console.log(arr.slice(3,1));
	console.log(arr.slice(100,102));
 

------

splice

splice(start,number to delete, ... ), used to add / remove element from array, and return the removed element array, the original array will be effect,

if second param is missing, then delete to end,

third and more params, it to be insert from start, (insert is perfromed after elements remove),

e.g.

	var arr = [1,2,3,4,5,6,7,8,9];

	console.log(arr);

	console.log(arr.splice(1,3));
	console.log(arr);

	console.log(arr.splice(4));
	console.log(arr);

	console.log(arr.splice(2,0,'a','b'));
	console.log(arr);

	console.log(arr.splice(2,2,'hello',' ','how are you!'));
	console.log(arr);
 

------

push & pop

push

append 1/more elements to the end, and return the new length of array, effect the original array,

pop

remove 1 element from the end, decrease length by 1, and return the removed element, effect the original array,

e.g.

	var arr = [1,2,3,4,5];

	console.log(arr);

	console.log(arr.push(6));
	console.log(arr);
	console.log(arr.push('a','b'));
	console.log(arr);

	console.log(arr.pop());
	console.log(arr);
 

------

shift & unshift

unshift

insert 1/more elements to the beginning, and return the new length, effect the original array,

if insert more elements is inserted once, their order in the new array is the same as in the param list,

shift

remove first elements of the array, and return it, effect the original array,

e.g.

	var arr = [1,2,3,4,5];

	console.log(arr);

	console.log(arr.unshift('a','b'));
	console.log(arr);

	console.log(arr.unshift('a'));
	console.log(arr.unshift('b'));
	console.log(arr);

	console.log(arr.shift());
	console.log(arr);
 

------

toString & toLocaleString

toString()

first convert its element to string if necessary, then join them with ',' into one string,

the output string is not surrounded by any thing,

this is the same as calling join(),

e.g.

		var arr = [1,2,3,4,5];
		var arr2 = [1,2,[3,[4,5]]];

		console.log(arr.toString());
		console.log(arr2.toString());
 

toLocaleString()

is the localized version of toString(),

first convert its element to string by calling toLocaleString() of the element if necessary, then join them with a locale-specific (and implementation-defined) separator into one string,

------

PS:       console.log();  is a build-in debug command supported by firefox & chrome,       so you can use firefox firebug console to test these code without write it into a js file,