JS数组的常用方法总结
来源:互联网 发布:权限管理的软件 编辑:程序博客网 时间:2024/05/16 19:32
-
-
-
- if(value instanceof Array){
-
- }
-
-
-
- if(Array.isArray(value)){
-
- }
-
-
-
- function isArray(value){
- return Object.prototype.toString.call(value) == "[object Array]";
- }
-
-
-
-
-
- var colors = ["a","b","c"];
- alert(colors.toString());
- alert(colors.valueOf());
- alert(colors);
-
-
-
-
-
- var colors2 = ["aa","bb","cc"];
- alert(colors2.join(","));
- alert(colors2.join("-"))
-
-
-
-
- var colors = new Array();
- var count = colors.push("red","green");
- alert(count);
-
- count = colors.push("black");
- alert(count);
-
- colors[3] = "blue";
- alert(colors.length);
-
-
- var item = colors.pop();
- alert(item);
-
-
-
-
-
- var colors = new Array();
- var count = colors.push("red","green");
- alert(count);
-
- count = colors.push("black");
- alert(count);
-
- var item = colors.shift();
- alert(item);
- alert(colors.length);
-
-
-
- var colors = new Array();
- colors.unshift("red","green");
- alert(colors);
-
- count = colors.unshift("black");
- alert(count);
-
- var item = colors.pop();
- alert(item);
-
-
-
-
- var values = [1,2,3,4,5];
- values.reverse();
- alert(values);
-
-
- var values2 = [0,1,5,10,15];
- values2.sort();
- alert(values2);
-
-
-
-
-
- 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);
-
- function sort_desc(v1,v2){
- if(v1 < v2){
- return 1;
- }else if(v1 > v2){
- return -1;
- }else{
- return 0;
- }
- }
-
- values2.sort(sort_asc);
- alert(values2);
-
-
- function compare(v1,v2){
- return v2 - v1;
- }
-
-
-
- var colors = ["red","green","blue"];
- var colors2 = colors.concat("yellow",["black","brown"]);
- alert(colors2);
-
-
- var colors = ["red","green","blue","yellow","purple"];
- var colors2 = colors.slice(1);
- var colors3 = colors.slice(1,4);
-
-
- var array = ["red","green","blue"];
- var removed = array.splice(0,1);
- alert(array);
- alert(removed);
-
-
- removed = array.splice(1,0,"yellow","orange");
- alert(array);
- alert(removed);
-
-
- removed = array.splice(1,1,"red","purple");
- alert(array);
- alert(removed);
-
-
-
- var numbers = [1,2,3,4,5,4,3,2,1];
- alert(numbers.indexOf(4));
- alert(numbers.lastIndexOf(4));
-
- alert(numbers.indexOf(4,4));
-
- var person = {name:"jack"};
- var people = [{name:"jack"}];
- var morePeople = [person];
-
- alert(people.indexOf(person));
- alert(morePeople.indexOf(person));
-
-
0 0