1 1---join() 2 //1----->join()方法将数组中所有元素都转化《《字符串》》 3 //并连接在一起 4 //array.join()方法是string.split()方法逆向操作 5 6 var a=[1,2,3];//创建一个包含三个元素的素组 7 var c=["多多","梦曦","洛瑶"]; 8 console.log(a.join()) 9 console.log(a.join(" ")) 10 console.log(a.join(""))
1 //2---->reverse() 将数组中元素颠倒顺序,返回逆序的数组 2 //下面的代码使用reverse 和join()方法生成字符串 3 var myarray=["狗狗","毛毛","多多","阿黄"] 4 console.log(myarray.reverse()); 5 //顺讯反转 ,以字符串输出结果 6 console.log(myarray.reverse().join())
1 //3---->array.sort()方法将数组中元素排序 2 //并返回排序后的数组, 3 //数组元素以字母表 4 5 var myArray=["dudu","xiaoxiao","diandian","bibi"]; 6 7 console.log(myArray.sort()) 8 console.log(myArray.sort().join()) 9 var MyArray=[3,2,1,5,6]; 10 console.log(MyArray.sort()) 11 var MyArray1=[111,44,66,77]; 12 MyArray1.sort(); 13 MyArray1.sort(function(a,b){ 14 return a-b; 15 16 }); 17 MyArray1.sort(function(a,b){ 18 return b-a; 19 });
1 //4----->concat() 连接数组,对数组合并 2 var MyArray2=[32,23,14,15,6]; 3 var MyArray3=["花花","梦瑶","叶子","欣欣"] 4 mm= MyArray2.concat(MyArray3,myArray) 5 console.log(mm) 6 c= MyArray2.concat(4,5,66) 7 console.log(c) 8 m=MyArray3.concat(["gouzi","小粒","多多"]) 9 console.log(m)
1 //5----->Arry.slice()方法返回指定数组的一个片段或子数组 2 //两个参数分别指定了开始的结束位置 3 MyArray4=["伤心地","梦瑶池","滴滴哒哒","小狐狸"] 4 d=MyArray4.slice(1,2).concat(MyArray3) 5 console.log(d)
1 //6----splice()方法是指在数组中插入或删除元素通用方法 2 //splice和slice拥有非常相似的名字 3 MyArray5=["香蕉","苹果","梨","花生","地瓜"] 4 console.log(MyArray5.splice(3)) 5 console.log(MyArray5.splice(1,3))
1 //7---push()和pop() 2 //---push()在末尾添加元素,并返回新的<<长度>> 3 //---pop()删除末尾的最后一个元素 4 console.log("push方法是添加字符,返回一个新的长度") 5 MyArray6=["狗儿","猫猫","小猪","小白"]; 6 console.log(MyArray6.push("小青蛇")) 7 MyArray7=["狗儿","猫猫","小猪","小白"]; 8 console.log("pop()删除末尾,返回的是删除的元素") 9 console.log(MyArray7.pop())
1 //8---->unshift() 和shift() 2 /*** 3 * unshift()在头部添加一个或多个元素,最后返回新的长度 4 * shift()删除数组第一个元素并将其返回 5 * 6 */ 7 MyArray8=["农夫三泉","怡宝","康师傅","和其正","金银花"] 8 console.log(MyArray8.unshift("雪碧","小花咪")) 9 MyArray8=["农夫三泉","怡宝","康师傅","和其正","金银花"] 10 console.log(MyArray8.shift()) 11 12 //---->forEach()方法从头至尾遍历数组