数组处理方法
//定义数组
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"]