廖雪峰官网学习js 数组
indexOf( ) 某字符的位置
slice 相当于string 的substring 切片
a = ['a','b',1,2,3] (5) ["a", "b", 1, 2, 3] a.slice(0,3) (3) ["a", "b", 1] a.slice(3) (2) [2, 3]
push() 后面追加 pop() 从后面删除
a.push('h') 7 a (7) ["a", "b", 1, 2, 3, 4, "h"] a.pop() "h" a (6) ["a", "b", 1, 2, 3, 4]
unshift() 在前面添加 shift()删除前面的
a.unshift(99) 7 a (7) [99, "a", "b", 1, 2, 3, 4] a.shift() 99 a (6) ["a", "b", 1, 2, 3, 4]
a.sort() 排序
reverse()翻转
splice(start, deleteCount, value, ...) 插入、删除或替换数组的元素 万能方法
obj.splice(n,
0
,val) 指定位置插入元素
obj.splice(n,
1
,val) 指定位置替换元素
obj.splice(n,
1
) 指定位置删除元素
a (6) [1, 2, 3, 4, "a", "b"] a.splice(2,0,8) [] a (7) [1, 2, 8, 3, 4, "a", "b"] a.splice(2,1) [8] a (6) [1, 2, 3, 4, "a", "b"] a.splice(2,1,8) [3] a (6) [1, 2, 8, 4, "a", "b"]
join()将数组元素构成字符串
a.join()
"1,2,8,4,a,b"
concat() 将两个数组相加
arr1=[1,2,3] arr2=[4,5,6] console.log(arr1.concat(arr2))