-
1. 如何清空數(shù)組
let arr = [1, 2, 3, 4, 5]; // 修改數(shù)組為空數(shù)組 arr = []; // 將數(shù)組的長度設(shè)為0 arr.length = 0; // 將數(shù)組從第0個數(shù)據(jù)刪到最后一個數(shù)據(jù) arr.splice(0, arr.length); console.log(arr);
-
2. 如何將數(shù)組轉(zhuǎn)換為字符串
let arr = [1, 2, 3, 4, 5]; // 調(diào)用.toString()方法 let str = arr.toString(); console.log(str); // 1,2,3,4,5 console.log(typeof str); // string
-
3. 如何將數(shù)組轉(zhuǎn)換成指定格式的字符串
let arr = [1, 2, 3, 4, 5]; // join方法默認情況下如果沒有傳遞參數(shù), 就是調(diào)用toString(); // join方法如果傳遞了參數(shù), 就會將參數(shù)作為元素和元素的連接符號 let str = arr.join("+"); console.log(str); // 1+2+3+4+5 console.log(typeof str); // string
-
4. 如何將兩個數(shù)組拼接為一個數(shù)組
-
concat() 方法
- 注意點: concat()方法不會修改原有的數(shù)組, 會生成一個新的數(shù)組返回給我們
let arr1 = [1, 3, 5]; let arr2 = [2, 4, 6]; // 格式: 數(shù)組1.concat(數(shù)組2); let res = arr1.concat(arr2); console.log(res); // [1, 3, 5, 2, 4, 6] console.log(typeof res); // object // 注意點: concat()方法不會修改原有的數(shù)組, 會生成一個新的數(shù)組返回給我們 console.log(arr1); console.log(arr2);
- 注意點: concat()方法不會修改原有的數(shù)組, 會生成一個新的數(shù)組返回給我們
-
擴展運算符
- 注意點: 擴展運算符也不會修改原有的數(shù)組, 也會生成一個新的數(shù)組返回給我們
let arr1 = [1, 3, 5]; let arr2 = [2, 4, 6]; // 注意點: 擴展運算符在解構(gòu)賦值中(等號的左邊)表示將剩余的據(jù)打包成一個新的數(shù)組 // 擴展運算符在等號右邊, 那么表示將數(shù)組中所有的數(shù)據(jù)解開, 放到所在的位置 let res = [...arr1, ...arr2]; // let res = [1, 3, 5, 2, 4, 6]; // 注意點: 擴展運算符也不會修改原有的數(shù)組, 也會生成一個新的數(shù)組返回給我們 console.log(arr1); console.log(arr2);
- 注意點: 擴展運算符也不會修改原有的數(shù)組, 也會生成一個新的數(shù)組返回給我們
-
注意點: 數(shù)組不能使用加號進行拼接, 如果使用加號進行拼接, 會先轉(zhuǎn)換成字符串再拼接
let arr1 = [1, 3, 5]; let arr2 = [2, 4, 6]; // 注意點: 數(shù)組不能使用加號進行拼接, 如果使用加號進行拼接, 會先轉(zhuǎn)換成字符串再拼接 let res = arr1 + arr2; let res = arr1.concat(arr2); console.log(res); // 1,3,52,4,6 console.log(typeof res); // string
-
concat() 方法
-
如何對數(shù)組中的內(nèi)容進行反轉(zhuǎn)
-
reverse() 方法
- 注意點: 會修改原有數(shù)組
let arr = [1, 2, 3, 4, 5]; // [1, 2, 3, 4, 5] [5, 4, 3, 2, 1] let res = arr.reverse(); console.log(res); // [5, 4, 3, 2, 1] // 注意點: 會修改原有數(shù)組 console.log(arr); // [5, 4, 3, 2, 1]
- 注意點: 會修改原有數(shù)組
-
reverse() 方法
-
如何截取數(shù)組中指定范圍內(nèi)容
-
slice() 方法
- 特點: slice方法是包頭不包尾(包含起始位置, 不包含結(jié)束的位置), 不會修改原有數(shù)組
let arr = [1, 2, 3, 4, 5]; // 0 1 2 3 4 // [1, 2, 3, 4, 5] // slice方法是包頭不包尾(包含起始位置, 不包含結(jié)束的位置) let res = arr.slice(1, 3); console.log(res); // slice()方法不會修改原有數(shù)組 console.log(arr);
- 特點: slice方法是包頭不包尾(包含起始位置, 不包含結(jié)束的位置), 不會修改原有數(shù)組
-
slice() 方法
-
如何查找元素在數(shù)組中的位置
-
indexOf() 方法
- 參數(shù)1: 需要查找的元素
- 參數(shù)2: 從什么位置開始查找
- 注意點: indexOf方法默認就是從左至右的查找, 一旦找到就會立即停止查找
// 0 1 2 3 4 5 let arr = [1, 2, 3, 4, 5, 3]; // indexOf方法如果找到了指定的元素, 就會返回元素對應(yīng)的位置 // 注意點: indexOf方法默認就是從左至右的查找, 一旦找到就會立即停止查找 let res = arr.indexOf(3); console.log(res); // 2 // indexOf方法如果沒找到指定的元素, 就會返回-1 let res = arr.indexOf(6); console.log(res); // -1 // 參數(shù)1: 需要查找的元素 // 參數(shù)2: 從什么位置開始查找 let res = arr.indexOf(3, 4); console.log(res); // 5
-
lastIndexOf()方法
- 參數(shù)1: 需要查找的元素
- 參數(shù)2: 從什么位置開始查找
- 注意點: lastIndexOf()方法默認就是從右至左的查找, 一旦找到就會立即停止查找
// 0 1 2 3 4 5 let arr = [1, 2, 3, 4, 5, 3]; // lastIndexOf方法如果找到了指定的元素, 就會返回元素對應(yīng)的位置 // 注意點: lastIndexOf方法默認就是從右至左的查找, 一旦找到就會立即停止查找 let res = arr.lastIndexOf(3); console.log(res); // 5 // lastIndexOf方法如果沒找到指定的元素, 就會返回-1 let res = arr.lastIndexOf(6); console.log(res); // -1 // 參數(shù)1: 需要查找的元素 // 參數(shù)2: 從什么位置開始查找 let res = arr.lastIndexOf(3, 4); console.log(res); // 2
-
indexOf() 方法
-
如何判斷數(shù)組中是否包含某個元素
-
indexOf() 方法和lastIndexOf() 方法
let arr = [1, 2, 3, 4, 4]; // 我們可以通過indexOf和lastIndexOf的結(jié)果, 判斷是否是-1即可, -1代表數(shù)組中不包含這個元素 let res = arr.indexOf(8); // let res = arr.lastIndexOf(8); console.log(res); // -1
-
includes() 方法
let arr = [1, 2, 3, 4, 4]; let res = arr.includes(8); console.log(res); // false let res = arr.includes(4); console.log(res); // true
-
indexOf() 方法和lastIndexOf() 方法