一、属性 length
var colors0=new Array();
var colors1=new Array(20);
var colors2=new Array('red','blue','green');
var colors3=[];
console.log(colors0.length);
console.log(colors1.length);
console.log(colors2.length);
console.log(colors2[0]);
colors2[1]='小明';
console.log(colors2[1]);
for(var i=0;i<colors2.length;i++){
console.log(colors2[i]);
}
1.尾部的增删 push()和pop()
var colors=new Array();
var count=colors.push('red','green')
console.log(colors);
count=colors.push('black');
console.log(colors);
var item=colors.pop();
console.log(item);
2头部的增删 shift()和unshift()
var colors=new Array();
var count=colors.push('red','green');
count=colors.push('black');
var item=colors.shift();
console.log(item);
console.log(colors.length);
var color=new Array();
var c=color.unshift('red','green');
c=color.unshift('black');
var i=color.pop();
console.log(i);
console.log(color.length);
3.重排序方法 sort()和reverse()
var values1=[1,2,3,4,5];
values1.reverse();
console.log(values1);
arr.sort(function(a,b){
return b-a;
});
console.log(arr);
4. 删除和拼接 slice()、splice()、concat()
var colors1=['red','blue','green'];
var colors2=colors1.concat('yellow',['black','brown']);
console.log(colors2);
var colors3=['red','green','blue','yellow','purple'];
var colors4=colors3.splice(1);
var colors5=colors3.splice(1,4);
console.log(colors4);
console.log(colors5);
var item=['red','blue','green'];
var removed=item.splice(0,1);
console.log(removed);
console.log(item);
removed=item.splice(1,0,'yellow','orange');
console.log(removed);
console.log(item);
removed=item.splice(1,1,'red','purple');
console.log(removed);
console.log(item);
5. 转换方法 toString()和join()
var colors=['red','green','blue'];
console.log(colors.toString());
var item=['red','blue','green'];
console.log(item.join('|'));
console.log(item.join('||'));
6. 迭代方法 some()、filter()、map()、forEach()、every()
var numbers=[1,2,3,4,5,4,3,2,1];
var everyResult=numbers.every(function(item,index,array){
return item>2;
});
console.log(everyResult);
var someResult=numbers.some(function(item,index,array){
return item>2;
});
console.log(someResult);
var filterResult=numbers.filter(function(item,index,array){
return item>2;
});
console.log(filterResult);
var mapResult=numbers.map(function(item,index,array){
return item*2;
});
console.log(mapResult);
numbers.forEach(function(item,index,array){
console.log(item);
})
7. 归并方法 reduce()和reduceRight()
var array=[1,2,3,4,5]
var sum1=array.reduce(function(prev,cur,index,array){
return prev+cur;
});
console.log(sum1);
var sum2=array.reduceRight(function(prev,cur,index,array){
return prev+cur;
});
console.log(sum2);
链接: https://www.cnblogs.com/jjgw/p/11608609.html.