數(shù)組高級API
- 遍歷數(shù)組的幾種方法
- 利用傳統(tǒng)循環(huán)來遍歷數(shù)組
for(let i = 0; i < arr.length; i++){ console.log(arr[i]); }
- 利用for in循環(huán)來遍歷數(shù)組
// 注意點: 在企業(yè)開發(fā)中不推薦使用forin循環(huán)來遍歷數(shù)組 for(let key in arr){ console.log(key); console.log(arr[key]); }
- 利用ES6中推出的for of循環(huán)來遍歷數(shù)組
for(let value of arr){ console.log(value); }
- 還可以利用Array對象的forEach方法來遍歷數(shù)組
forEach方法會自動調(diào)用傳入的函數(shù) // 每次調(diào)用都會將當前遍歷到的元素和當前遍歷到的索引和當前被遍歷的數(shù)組傳 遞給這個函數(shù) arr.forEach(function (currentValue, currentIndex, currentArray) { // console.log(currentValue, currentIndex, currentArray); console.log(currentValue); });
- 數(shù)組的查找方法
- findIndex方法
findIndex方法: 定制版的indexOf, 找到返回索引, 找不到返回-1 let index = arr.findIndex(function (currentValue, currentIndex, currentArray) { // console.log(currentValue, currentIndex, currentArray); // if(currentValue === 6){ if(currentValue === 10){ return true; } }); console.log(index);
- find方法
find方法如果找到了就返回找到的元素, 如果找不到就返回undefined let value = arr.find(function (currentValue, currentIndex, currentArray) { // console.log(currentValue, currentIndex, currentArray); // if(currentValue === 6){ if(currentValue === 10){ return true; } }); console.log(value);
數(shù)組的添加方法
- filter方法
將滿足條件的元素添加到一個新的數(shù)組中
let newArray = arr.filter(function (currentValue, currentIndex, currentArray) {
// console.log(currentValue, currentIndex, currentArray);
if(currentValue % 2 === 0){
return true;
}
});
console.log(newArray); // [2, 4]
- map方法
將滿足條件的元素映射到一個新的數(shù)組中
let newArray = arr.map(function (currentValue, currentIndex, currentArray) {
// console.log(currentValue, currentIndex, currentArray);
if(currentValue % 2 === 0){
return currentValue;
}
});
console.log(newArray); // [undefined, 2, undefined, 4, undefined]
- 數(shù)組的排序方法
let arr = ["c", "a", "b"];
arr.sort();
/*
如果 compareFunction(a, b) 小于 0 棺聊,那么 a 會被排列到 b 之前;
如果 compareFunction(a, b) 等于 0 贞谓, a 和 b 的相對位置不變限佩。
如果 compareFunction(a, b) 大于 0 , b 會被排列到 a 之前
注意點: 如果元素是字符串類型, 那么比較的是字符串的Unicode編碼
*/
arr.sort(function (a, b) {
if(a > b){
return -1;
}else if(a < b){
return 1;
}else{
return 0;
}
});
console.log(arr);
- 規(guī)律
- 如果數(shù)組中的元素是數(shù)值類型
- 如果需要升序排序, 那么就返回a - b;
- 如果需要降序排序, 那么就返回b - a;
字符串常用方法
- 獲取字符串長度
let str = "abcd";
console.log(str.length);
- 獲取某個字符 [索引] / charAt
let str = "abcd";
let ch = str[1];
let ch = str.charAt(1);
console.log(ch);
- 字符串查找 indexOf / lastIndexOf / includes
let str = "vavcd";
let index = str.indexOf("v");
let index = str.lastIndexOf("v");
console.log(index);
let result = str.includes("p");
console.log(result);
- 拼接字符串 concat / +
let str1 = "www";
let str2 = "it666";
let str = str1 + str2; // 推薦
let str = str1.concat(str2);
console.log(str);
- 截取子串 slice / substring / substr
let str = "abcdef";
let subStr = str.slice(1, 3);
let subStr = str.substring(1, 3);
let subStr = str.substr(1, 3);
console.log(subStr);
- 字符串切割
let arr = [1, 3, 5];
let str = arr.join("-");
console.log(str);
let str = "1-3-5";
let arr = str.split("-");
console.log(arr);
- 判斷是否以指定字符串開頭 ES6
let str = "http://www.it666.com";
let result = str.startsWith("www");
console.log(result);
- 判斷是否以指定字符串結(jié)尾 ES6
let str = "lnj.jpg";
let result = str.endsWith("png");
console.log(result);
- 字符串模板 ES6
let name = "lnj";
let age = 34;
// let str = "我的名字是" + name + ",我的年齡是" + age;
let str = `我的名字是${name},我的年齡是${age}`;
console.log(str);