數組處理方法
//定義數組
var array = [];
undefined
//查看類型
typeof(array);
"object"
//往數組里添加數據
array = ['first','second','third']
["first", "second", "third"]
//數組長度
array.length
3
//索引
array[0]
"first"
//添加數組新內容
array[3] = 'fourth'
"fourth"
array
["first", "second", "third", "fourth"]
//使用push添加數組的新數據,是往數組后添加數據
array.push('fifth','sixth')
6
array
["first", "second", "third", "fourth", "fifth", "sixth"]
//往前添加是使用unshift-------------------
var newArray = ["second", "three", "four", "one", "two"]
newArray.unshift('haha')
6
newArray
["haha", "second", "three", "four", "one", "two"]
//-----------------------
//刪除數據最后一個元素使用pop
array.pop()
"sixth"
array
["first", "second", "third", "fourth", "fifth"]
///刪除數組第一個元素使用shift
array.shift()
"first"
///刪除數組中某一個元素的值會使用delete,不會刪除數組中該元素
delete array[3]
true
array
["second", "third", "fourth", undefined × 1]
//徹底刪除數組里的元素使用splice方法
array.splice(3)
[undefined × 1]
array
["second", "third", "fourth"]
//合并兩個數組使用concat
var array2 = ['one','two']
undefined
var newArray = array.concat(array2)
undefined
newArray
["second", "third", "fourth", "one", "two"]