var name = new Array("Greg");
var name = Array("Greg");
var colors =["red","blue","green"];
colors[colors.length] = "black";
console.log(colors.length,colors);
//colors.join(",");
console.log(colors.join(","));
console.log(colors.join("||"));//数组转换成字符串
var colors = new Array();
var count = colors.push("red","green");//末尾添加
console.log(count,colors);
count = colors.push("black","gray");
console.log(count,colors);
var item = colors.pop(); //末尾删除 栈方法(后进先出 LIFO)
console.log(item,colors.length);
var item2 = colors.shift();//前端删除
console.log(item2,typeof item2,item2.length,colors.length,colors);
var item3 = colors.unshift("yellow");//前端添加(先进先出 FIFO
console.log(item3,colors);
var colors = new Array();
var count = colors.unshift("red","green");
console.log(count,colors);
count =colors.unshift("black");
console.log(count,colors);
var item = colors.pop();
console.log(item,colors.length,colors);
var values=[1,2,3,4,5];
values.reverse();
console.log(values);
var values = [0,1,5,10,15];
values.reverse();
console.log(values);
var values = [0,1,5,10,15];
values.sort();
console.log(5 < 10,"5" < "10");//ture ,false 根据unicode编码比较
console.log(values,values.toString());
// 此方法
function compare(value1,value2){
if(value1 < value2){
return -1;
console.log(1);
}else if(value1 > value2){
return 1;
console.log(2);
}else{
return 0;
console.log(3);
}
}
values.sort(compare)
console.log(values);
//更简单的方法
function compare2(value1,value2){
return value2-value1;
}
values.sort(compare2);
console.log(values);
/**concat
*创建当前数组的一个副本,然后将接受的参数添加到这个副本的的
*末尾,最后返回一个新构建的数组。
*/
var colors = ["red","green","blue","gray"];
/**concat
*创建当前数组的一个副本,然后将接受的参数添加到这个副本的的
*末尾,最后返回一个新构建的数组。
*/
var colors = ["red","green","blue","gray"];
var colors2 = colors.concat("yellow",["black","brown"]);
console.log(colors);
console.log(colors2);
var cl = colors.concat();
cl.push("aa","bb")
console.log(colors,cl);
slice 基于当前数组中的一项或多个项创建一个新的数组
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,3);
var colors4 = colors.slice(-3,-1);//起始位置为-3
console.log(colors,colors2);
console.log(colors3,colors4);
/**splice 删除(起始,删除的项数)
*插入(起始,0【删除的项数】,要插入的项数)
*替换(起始,要删除的项数,要插入任意数量的项)
*/
console.log(colors);
var colors5 = colors.splice(0,2);
var colors6 = colors.splice(1,0,"aa","bb","cc");
var colors7 = colors.splice(1,3,"yellow","black","white")
console.log(colors);//
console.log(colors5,colors6);//返回删除的两项
console.log(colors,colors7);
/** 位置方法indexOf lastIndexOf
*
*/
var numbers = [1,2,3,4,5,4,3,2,1];
var nn=[]
console.log(numbers.indexOf(3));
//nn.push(numbers.indexOf(3,1))
console.log(numbers.indexOf(3,4) );
console.log(numbers.lastIndexOf(1));
console.log(numbers.lastIndexOf(4,4));
var person = { name:"Nicholas"};
var people = [{name:"Nicholas"}];
var morePeople=[person];
console.log(people.indexOf(person));
console.log(morePeople.indexOf(person));
// 迭代方法
//每一项都返回true,则返回true
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);
//数组每一项给定的函数,有一项都返回true,则返回true
var someResult = numbers.some(function(item,index,array){
return (item > 2);
});
console.log(someResult);
//数组每一项给定的函数,每一项都返回true,则返回true项组成的数组
var filterResult = numbers.filter(function(item,index,array){
//console.log(item,index,array);
return (item > 2);
});
console.log( filterResult );
//数组每一项给定的函数,每一项都返回true,则返回true项组成的数组
var mapResult = numbers.map(function(item,index,array){
//console.log(item,index,array);
return (item*2);
});
console.log( mapResult );
// 数组每一项给定的函数,没有返回值
var foreachReault = numbers.foreach(function(item,index,array){
});
console.log( foreachReault );