1、join (separator):通過分隔符將數組轉換爲字符串;
separator:可選,指定的分隔符,如果省略,則默認使用逗號;
例:
let arry = [1, 2, 3, 4]
let newEnd = arry.join(',')
console.log(newEnd) // 1,2,3,4
console.log(typeof (newEnd)) // string
console.log(arry) // [1, 2, 3, 4]
2、splice(index,del,additem1,...additemn) : 向數組中添加/刪除項目,然後返回被刪除的項目;
index : 必須,整數,規定添加/或刪除元素的位置,使用負數則從結尾開始;
del : 必須,整數,刪除元素的數量,0則不刪除元素;
additem1 : 可選,向數組添加的元素;
例:
let arry = [1, 2, 3, 4]
let newEnd = arry.splice(2, 1, 5)
console.log(newEnd) // [3]
console.log(arry) // [1, 2, 5, 4]
注:splice()該方法會改變原數組,返回值爲被刪除元素的數組,如果未刪除,則返回空數組;
3、slice(start,end) : 從已有的數組中返回選定的元素,包含從 start 到 end (不包括該元素);數組字符串共同方法
start: 必須,從何處開始選取,-1表示從尾部開始算起;
end : 可選,從何處結束選取,如果忽略此參數,那麼切分的數組包含從 start 到數組結束的所有元素。負數表示從尾部開始。
例:
let arry = [1, 2, 3, 4, 5]
let newEnd = arry.slice(1, 4)
console.log(newEnd) // [2, 3, 4]
console.log(arry) // [1, 2, 3, 4, 5]
注:slice()該方法不會改變原數組,返回值爲新選定元素的數組;
4、push () 方法可向數組的末尾添加一個或多個元素,並返回新的長度。(改變原數組)
studyFun() {
let arr = [1, 2, 3, 4]
let end = arr.push(5, 6)
console.log(end)// 6
console.log(arr)//[1, 2, 3, 4, 5, 6]
},
5、unshift()該方法向數組的開頭添加一個或更多元素,並返回新的長度。(改變原數組)
studyFun() {
let arr = [1, 2, 3, 4]
let end = arr.unshift(88,77)
console.log(end) // 6
console.log(arr) //[88, 77, 1, 2, 3, 4]
},
6、pop()該方法用於刪除數組的最後一個元素,並返回被刪除的元素(改變原數組的長度)
studyFun() {
let arr = [1, 2, 3, 4]
let end = arr.pop()
console.log(end)// 4
console.log(arr)//[1, 2, 3]
},
7、shift()該方法用於刪除數組的第一個元素,並返回被刪除的元素(改變原數組的長度)
studyFun() {
let arr = [1, 2, 3, 4]
let end = arr.shift()
console.log(end) // 1
console.log(arr) //[2, 3,4]
},
太菜,基礎太差腫麼辦?
今天,提離職了,內心很難過,也有很多的不捨,可是。。。