js基础(数组)
//数组 var arr = new Array('1','2','3','4') //遍历数组 for(var i=0;i<arr.length;i++){ console.log(arr[i]) } //在结尾添加数组元素:arr.push();返回值为新数组长度 var arr1 = new Array(1,2,3) arr1.push(4) console.log(arr1) //[1,2,3,4] //在数组开头添加新元素:unshift(); arr1.unshift(0) console.log(arr1) //[0,1,2,3,4] //删除数组最后一个元素:pop();返回值是删除的元素 arr1.pop() console.log(arr1) //[0,1,2,3] //删除数组第一个元素:shift(); arr1.shift() console.log(arr1) //[123] //更改元素:a[i] = value; arr1[0] = 11 console.log(arr1) //[11,2,3] //数组转字符串:toString();//使用的是逗号分隔符; var s = arr1.toString() console.log(s) //11,2,3 //数组元素结合成字符串:join(“*”);//自定义分隔符; var a = arr1.join('^') console.log(a) // 11^2^3 //字符串转数组:string.split(“ ”); var b = '123456' var c = b.split(""); console.log(c) //[1,2,3,4,5,6] var str="How are you doing today?" document.write(str.split(" ") + "<br />") //How,are,you,doing,today? document.write(str.split("") + "<br />") //H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,? document.write(str.split(" ",3)) //How,are,you "2:3:4:5".split(":") //["2", "3", "4", "5"] "|a|b|c".split("|") //["", "a", "b", "c"] "hello".split("", 3) // ["h", "e", "l"] //arr.indexOf(元素)数组中某个指定的元素位置;包含某个元素>=0; -1 console.log(arr1.indexOf(2))