JS数组的常用方法总结

来源:互联网 发布:网络大电影商业模式 编辑:程序博客网 时间:2024/04/28 10:28
//【检测数组】//1.对于一个网页,或者一个全局作用域而言,使用instaceof操作符if(value instanceof Array){//todo...}//2.如果网页中包含多个框架,那实际上存在2个以上不同的全局执行环境//ECMAScipt5新增了 Array.isArray()方法,最终确定某个值到底是不是数组,而不管它在哪个全局执行环境中创建的if(Array.isArray(value)){//todo...}//支持Array.isArray()方法的浏览器有IE9+、Firefox 4+、Safari 5+、Opera 10.5+ 和Chrome//3.在尚未实现Array.isArray()方法的浏览器中准确的检测数组function isArray(value){return Object.prototype.toString.call(value) == "[object Array]";}

//【转换方法】//toLocaleString()、toString() 和 valueOf()//1.调用数组的 toString()方法会返回由数组中的每个值的字符串形式拼接而成的一个以逗号分隔的字符//而调用valueOf()返回的还是数组var colors = ["a","b","c"];alert(colors.toString()); //a,b,calert(colors.valueOf());//a,b,calert(colors);//a,b,c//说明:由于alert()要接收字符串参数,所以它会在后台调用toString()方法,由此会得到与直接调用toString()方法相同的结果//2.使用join()方法,可以使用不同的分隔符来构建这个字符串var colors2 = ["aa","bb","cc"];alert(colors2.join(","));//aa,bb,ccalert(colors2.join("-"))//aa-bb-cc
//【栈方法】//栈是一种后进先出的数据结构,也就是最新添加的项最早被移除。而栈中项的插入和移除,只发生在栈的顶部。//1.push()方法可以接受任意数量的参数,把它们逐个添加到数组的末尾,并返回数组修改后的长度var colors = new Array();    //创建一个数组var count = colors.push("red","green"); //推入2项alert(count);//2count = colors.push("black");//推入另1项alert(count);//3colors[3] = "blue";alert(colors.length);//4//2.pop()方法,弹出数组末尾的一项var item = colors.pop();alert(item);//"blue"
//【队列方法】//队列数据结构的访问规则是先进先出,在列表末端添加项,从列表前端移除项//1.shift(),移除数组中第一项并返回该项,同时将长度减1//结合使用shift()和push()方法,可以像使用队列一样使用数组var colors = new Array();var count = colors.push("red","green");//推入2项alert(count);//2count = colors.push("black");//推入另一项alert(count);//3var item = colors.shift();alert(item);//"red"alert(colors.length);//2//2.unshift(),在数组前端添加任意个项,并返回数组的长度var colors = new Array();colors.unshift("red","green");alert(colors); //"red","green"count = colors.unshift("black");alert(count); //3var item = colors.pop();alert(item); //"black"

//【重排序方法】//1.反转数组的顺序var values = [1,2,3,4,5];values.reverse();alert(values);//5,4,3,2,1//2.sort()方法按升序排列数组项——即最小的值位于最前面,最大的值在最后面var values2 = [0,1,5,10,15];values2.sort();alert(values2);//0,1,10,15,5/*sort()方法会调用每个数组元素的toString()转型方法,然后比较得到的字符串这种排序方式在很多情况下都不是最佳方案*///3.sort()方法可以接收一个比较函数作为参数function sort_asc(v1,v2){if(v1 < v2){return -1;}else if(v1 > v2){return 1;}else{return 0;}}values2.sort(sort_asc);alert(values2);//0,1,5,10,15function sort_desc(v1,v2){if(v1 < v2){return 1;}else if(v1 > v2){return -1;}else{return 0;}}values2.sort(sort_asc);alert(values2);//15,10,5,1,0//对于数值类型或者其valueOf()方法返回数组类型的对象类型function compare(v1,v2){return v2 - v1;}
//【操作方法】//1.concat()方法,可以基于当前数组中的所有项创建一个新数组var colors = ["red","green","blue"];var colors2 = colors.concat("yellow",["black","brown"]);alert(colors2); //red,green,blue,yellow,black,brown//2.截取数组var colors = ["red","green","blue","yellow","purple"];var colors2 = colors.slice(1);//green,blue,yellow,purplevar colors3 = colors.slice(1,4);//green,blue,yellow//3.删除var array = ["red","green","blue"];var removed = array.splice(0,1);//删除第一项alert(array);//green,bluealert(removed);//red//4.添加removed = array.splice(1,0,"yellow","orange"); //从位置1开发插入2项alert(array); //green,blue,yellow,orangealert(removed); //返回一个空数组//5.替换removed = array.splice(1,1,"red","purple"); //插入两项,删除一项alert(array);//green,red,purple,orange,bluealert(removed); //yellow
//【位置方法】//1.查找某某项在数组中的位置var numbers = [1,2,3,4,5,4,3,2,1];alert(numbers.indexOf(4));//3alert(numbers.lastIndexOf(4));  //5alert(numbers.indexOf(4,4));//5 参数(查找项,查找起点位置索引)var person = {name:"jack"};var people = [{name:"jack"}];var morePeople = [person];alert(people.indexOf(person));//-1alert(morePeople.indexOf(person)); //0/*indexOf()和lastIndexOf()方法不支持IE9以下*/





0 0
原创粉丝点击