js array常用操作
2481 ワード
js Array :
splice():
splice(0,2)
splice(2,0,"red","green") 2 "red" "green"
splice (2,1,"red","green") 2 , 2
"red" "green"
:
: 、 0( )
new Array():
length:
push():
indexOf(): , -1
pop(): ( )
shift(): ( )
unshift(): ,
join():
valueOf():
sort():
reverse():
concat(): :
var colors = ["red", "green", "blue"];
var colors2 = colors.concat("yellow", ["black", "brown"]);
slice(): ,
slice(0): , copy
slice(0,3): , 3
Array.isArray():
:
every(): , true, true。
var numbers = [1,2,3,4,5,4,3,2,1];
var everyResult = numbers.every(function(item, index, array){
return (item > 2);
});
alert(everyResult); //false
var someResult = numbers.some(function(item, index, array){
return (item > 2);
});
alert(someResult); //true
filter(): , true 。
var numbers = [1,2,3,4,5,4,3,2,1];
var filterResult = numbers.filter(function(item, index, array){
return (item > 2);
});
alert(filterResult); //[3,4,5,4,3]
forEach(): 。 。
var numbers = [1,2,3,4,5,4,3,2,1];
numbers.forEach(function(item, index, array){
//
});
map(): , 。
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
return item * 2;
});
alert(mapResult); //[2,4,6,8,10,8,6,4,2]
reduce() reduceRight()。
, ;
reduce() ,
。 reduceRight() ,
var values = [1,2,3,4,5];
var sum = values.reduce(function(prev, cur, index, array){
return prev + cur;
});
alert(sum); //15
var values = [1,2,3,4,5];
var sum = values.reduceRight(function(prev, cur, index, array){
return prev + cur;
});
alert(sum); //15