javascript----数组的方法

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


下一篇:[转]使用Stopwatch类实现高精度计时