JavaScript 数组处理方法总结

来源:互联网 发布:未闻花名网络歌手歌词 编辑:程序博客网 时间:2024/05/17 16:44
数组处理方法
  1. //定义数组
  2. var array = [];
  3. undefined
  4. //查看类型
  5. typeof(array);
  6. "object"
  7. //往数组里添加数据
  8. array = ['first','second','third']
  9. ["first", "second", "third"]
  10. //数组长度
  11. array.length
  12. 3
  13. //索引
  14. array[0]
  15. "first"
  16. //添加数组新内容
  17. array[3] = 'fourth'
  18. "fourth"
  19. array
  20. ["first", "second", "third", "fourth"]
  21. //使用push添加数组的新数据,是往数组后添加数据
  22. array.push('fifth','sixth')
  23. 6
  24. array
  25. ["first", "second", "third", "fourth", "fifth", "sixth"]
  26. //往前添加是使用unshift-------------------
  27. var newArray = ["second", "three", "four", "one", "two"]
  28. newArray.unshift('haha')
  29. 6
  30. newArray
  31. ["haha", "second", "three", "four", "one", "two"]
  32. //-----------------------
  33. //删除数据最后一个元素使用pop
  34. array.pop()
  35. "sixth"
  36. array
  37. ["first", "second", "third", "fourth", "fifth"]
  38. ///删除数组第一个元素使用shift
  39. array.shift()
  40. "first"
  41. ///删除数组中某一个元素的值会使用delete,不会删除数组中该元素
  42. delete array[3]
  43. true
  44. array
  45. ["second", "third", "fourth", undefined × 1]
  46. //彻底删除数组里的元素使用splice方法
  47. array.splice(3)
  48. [undefined × 1]
  49. array
  50. ["second", "third", "fourth"]
  51. //合并两个数组使用concat
  52. var array2 = ['one','two']
  53. undefined
  54. var newArray = array.concat(array2)
  55. undefined
  56. newArray
  57. ["second", "third", "fourth", "one", "two"]
0 0
原创粉丝点击