数组的23个方法

来源:互联网 发布:网络连接器是什么意思 编辑:程序博客网 时间:2024/05/21 11:06

  数组的23个方法
  push

   //返回长度
   var arr = new Array(3)
   arr[0] = "George"
   arr[1] = "John"
   arr[2] = "Thomas" 
   document.write(arr + "<br />");//George,John,Thomas
   document.write(arr.push("James") + "<br />");//4
   document.write(arr);//George,John,Thomas,James
  unshift
  pop

   //返回删除的元素
   var arr = new Array(3)
   arr[0] = "George"
   arr[1] = "John"
   arr[2] = "Thomas"
   document.write(arr)//George,John,Thomas
   document.write("<br />")
   document.write(arr.pop())//Thomas
   document.write("<br />")
   document.write(arr)//George,John
  shift
  concat

   var a = [1,2,3];
   document.write(a.concat(4,5));//1,2,3,4,5
  
  slice
   var arr = new Array(6)
   //不会改变原数组
   arr[0] = "George"
   arr[1] = "John"
   arr[2] = "Thomas"
   arr[3] = "James"
   arr[4] = "Adrew"
   arr[5] = "Martin"
   document.write(arr + "<br />")//George,John,Thomas,James,Adrew,Martin
   document.write(arr.slice(2,4) + "<br />")//Thomas,James
   document.write(arr)//George,John,Thomas,James,Adrew,Martin
  splice
   //删除从 index 2 ("Thomas") 开始的三个元素,并添加一个新元素 ("William") 来替代被删除的元素
   var arr = new Array(6)
   arr[0] = "George"
   arr[1] = "John"
   arr[2] = "Thomas"
   arr[3] = "James"
   arr[4] = "Adrew"
   arr[5] = "Martin"
   document.write(arr + "<br />")//George,John,Thomas,James,Adrew,Martin
   arr.splice(2,3,"William")
   document.write(arr)//George,John,William,Martin
  
  sort
  reverse
  
  toString

   //把一个逻辑值转换为字符串,并返回结果
   var boo = new Boolean(true)
   document.write(boo.toString())//true
  
  indexOf
   var str="Hello world!"
   //返回某个指定的字符串值在字符串中首次出现的位置,大小写敏感
   document.write(str.indexOf("Hello") + "<br />")//0
   document.write(str.indexOf("World") + "<br />")//-1 找不到返回-1
   document.write(str.indexOf("world"))//6
  lastIndexOf
   //从尾到头地检索字符串 stringObject
  
  toLocalString
  valueOf

   //valueOf() 方法可返回 Boolean 对象的原始值。
   var boo = new Boolean(false)
   document.write(boo.valueOf())//false
  split
   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
  join
   var arr = new Array(3)
   arr[0] = "George"
   arr[1] = "John"
   arr[2] = "Thomas"
   document.write(arr.join("."))//George.John.Thomas
  
  map
  every
  forEach
  some
  filter
  
  reduce
  reduceRight

  
0 0
原创粉丝点击