1 isNaN(value) 2 3 typeof value 4 5 value instanceof Array 6 7 Array.isArray(valueName) // ie9+ 8 9 valueName.toString() 10 11 valueName.valueOf() 12 13 valueName.toLocaleString() 14 15 valueName.join("|")//以什么格式分开,如果值数组值为null,undefined 已空字符串格式分开 16 17 //例子 18 19 var arr = ["red", "blue"] //注意最后不要加逗号, ie8及以下为认为3个值,第三个值为“undefined” 20 21 arr[100] = "green"; 22 23 alert(arr.join("|")) 24 25 26 27 var colors = []; 28 29 var count = colors.push("red", "green"); //添加两项 30 31 alert(count) // 2 32 33 count = colors.push("black"); 34 35 alert(count) //3 p.s 当前数组追加后的length 36 37 38 39 var item = colors.pop() // 取得最后一项(数组也少了) 40 41 alert(item) // "black" 42 43 alert(colors.length) //2 44 45 46 47 var colors = ["red", "green","black"]; 48 49 var item = colors.shift() //取得第一项 50 51 alert(item) // "red" 52 53 alert(colors.length) // 2 54 55 56 57 var colors = []; 58 var counts = colors.unshift("red","green"); //往前追加两项 59 alert(colors.length) //2 60 61 62 63 var value = [1,2,3,4,5]; 64 value.reverse(); 65 alert(value) //5,4,3,2,1 66 67 68 //reverse()和sort()方法的返回都是经过排序之后的数组 69 var value = [0,1,5,10,15] 70 value.sort(); 71 alert(value) //0,1,10,15,5 72 73 74 75 function compare(value1, value2){ 76 if(value1 < value2){ 77 return 1; 78 }else if(value1 > value){ 79 return -1; 80 }else{ 81 return 0; 82 } 83 } 84 var values = [0,1,5,10,15] //如果数组里用相同的或者非字符串救回报错(me) 85 values.sort(compare); 86 alert(values) // 15,10,5,1,0 87 88 } 89 90 91 92 //简化 93 94 function compare(value1, value2){ 95 return value2 - value1 96 } 97 98 //也可以(chrome可以 ie有问题)[me] 99 100 function compare(value1, value2){ 101 return value2 < value1 102 } 103 104 105 106 //数组链接 107 var colors = ["red", "green", "blue"] 108 var color2 = ["yellow",["black","brown"]] 109 var color2 = colors.concat(color2); 110 alert(color2) //"red", "green", "blue","yellow","black","brown" 111 alert(color2.length) //5 112 //注意区分 113 var arr = [ 114 [1,2,3], 115 [4,5,6] 116 ] 117 alert(arr.length) //2 118 119 120 121 //获取,slice()方法不会影响原始数组 122 123 var arr = [1,2,3,4,5] 124 var arr1 = arr.slice(); 125 var arr2 = arr.slice(1); 126 var arr3 = arr.slice(1,4) //不包括4 127 128 alert(arr1) // 1,2,3,4,5 129 alert(arr2) // 2,3,4,5 130 alert(arr3) // 2,3,4 131 132 alert(arr.slice(-2,-1)) //4 相当于slice(3,4) ,如果结束为止小于起始位置,如slice(-2,-3),结果返回空数组, 负数可以理解从右往左,【me】 133 134 135 136 //splice("开始位置","几个",Array)功能强大,包括删除、插入、替换 137 // --删除 138 139 var colors = ["red","blue","green"] 140 var remove = colors.slice(0,1); //删除第一项 141 alert(remove) //返回的数组中包含几项 142 alert(colors) //"blue","green" 143 144 // --插入 145 146 var colors = ["red","blue","green"] 147 var remove = colors.splice(1,0,"yellow"); //往第二个添加yellow 148 alert(remove) //返回的是一个空数组 149 alert(colors) //red,yellow,blue,green 150 151 // --替换 152 153 var colors = ["red","blue","green"] 154 var remove = colors.splice(1,1,"yellow","black"); //把第二个替换成yellow,black 155 alert(remove) //返回的是一个空数组 156 alert(colors) //red,yellow,black,green 157 158 159 //位置方法 array 使用indexOf lastIndexOf 只有ie9+等 160 161 var arr= ["a","b","c","d","e","a"]; 162 alert(arr.indexOf("a")) //0 "a"在哪个位置上 163 alert(arr.lastIndexOf("a")) //5 最后一个"a"在哪个位置上 164 165 alert(arr.indexOf("f")) //-1 没有返回-1