选择排序、json对象、indexof、回调函数、ES5新增遍历函数、字符串定义、asc码表、字符串API
選擇排序
?? ?var t;
?? ?var k;
? ? 核心算法
? ?外層循環(huán)n-1
?? ?for(var i=0; i<arr.length-1; i++){
?? ??? ?k = i;
?? ??? ?//內(nèi)層循環(huán)n-i-1
?? ??? ?for(var j=i+1; j<arr.length; j++){
?? ??? ??? ?if(arr[k]<arr[j]){
?? ??? ??? ??? ?k = j;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?t = arr[i];
?? ??? ?arr[i] = arr[k];
?? ??? ?arr[k] = t;
?? ?}
?? ?
?? ?console.log(arr);
?json對象
json對象:是一種描述數(shù)據(jù)結構格式的語法規(guī)則,
?? ?將若干個屬性和行為封裝成一個整體
?? ?a.定義:由鍵值對構成
?? ?var stu = {
?? ?? ? ?key1:value1,
?? ?? ? ?key2:value2...
?? ??}
b.訪問屬性
? ?方法一:
? ?對象名.屬性名
?? ?console.log(stu.name, stu.age, stu.score);
? 方法二:
? 下標法
?? ?var str = "score";
?? ?console.log(stu["name"], stu["age"], stu[str]);
? ? c.添加自定義屬性
?? ?stu.hobby = "美女";
?? ?stu["tall"] = "180";
?? ?console.log(stu.hobby, stu["tall"]);
json屬性的遍歷
?? ?//for...in...遍歷下標
?? ?for (var index in stu) {
?? ??? ?//for...in...不能支持點運算符訪問屬性
?? ??? ?//console.log(stu.index);
?? ??? ?console.log(stu[index]);
?? ?}?
?indexof
indexOf
?? ??功能:查找目標元素,
? ? 參數(shù):indexOf(目標元素)
? ? 返回值:找到返回目標元素的下標,找不到返回-1
?? ?
? ? ?var arr = [5,6,4,7,8];
? ?console.log(arr.indexOf(14));
回調(diào)函數(shù)
回調(diào)函數(shù):一個函數(shù)當做另一個函數(shù)的參數(shù),被當做參數(shù)的函數(shù)是回調(diào)函數(shù)
目的可以將一個函數(shù)(功能模塊傳遞至另一個函數(shù)體內(nèi))
ES5新增的遍歷函數(shù)
? ?forEach
?? ?/功能:將數(shù)組的所有元素進行遍歷,執(zhí)行某種操作,
?? ?// 參數(shù):forEach(回調(diào)函數(shù))
?? ?// 回調(diào)函數(shù)(元素的數(shù)值,[元素的下標],[元素所在的數(shù)組])
?? ?// 返回值:無
?? ?
?? ?// var arr = [6,5,7,4,8];
?? ?// function fun(x,index,a){
?? ?// ?? ?//console.log(x);
?? ?// ?? ?a[index] += 10;
?? ?// }
?? ?// arr.forEach(fun);
?? ?// console.log(arr);
?? ?
?? ?//map:和forEach幾乎一毛一樣,但是有返回值,
?? ?// 功能:將數(shù)組的所有元素進行遍歷,執(zhí)行某種操作,
?? ?// 參數(shù):map(回調(diào)函數(shù))
?? ?// 回調(diào)函數(shù)(元素的數(shù)值,[元素的下標],[元素所在的數(shù)組])
?? ?// 返回值:通過回調(diào)函數(shù)的return返回值,組成一個新的數(shù)組
?? ?// var arr = [6,5,7,4,8];
?? ?// function fun(x,index,a){
?? ?// ?? ?//console.log(x);
?? ?// ?? ?a[index] += 10;
?? ?// ?? ?return a[index];
?? ?// }
?? ?// var arr1 = arr.map(fun);
?? ?// console.log(arr1);
?? ?
? ?filter:
? ?功能:過濾元素,
? ? 參數(shù):filter(回調(diào)函數(shù))
?? ?回調(diào)函數(shù)(元素的數(shù)值,[元素的下標],[元素所在的數(shù)組])
? ? ?返回值:根據(jù)回調(diào)函數(shù)返回的布爾值,創(chuàng)建新的數(shù)組,
?? ?var arr = [6,5,7,4,8];
?? ?function fun(x){
?? ??? ?if(x%2){
?? ??? ??? ?return true;
?? ??? ?}else{
?? ??? ??? ?return false;
?? ??? ?}
?? ?}
?? ?var arr1 = arr.filter(fun);
?? ?console.log(arr1);
字符串定義;
var str1 = "heihei";//內(nèi)置基本類型
?? ?var str2 = new String("heihei");//引用類型
?? ?console.log(typeof str1);
?? ?console.log(typeof str2);?
asc碼表
a=97 A=65? space=32? 0=48? ?回車13
charAt
? ? 功能:返回索引對應的字符,
? ? 參數(shù):charAt(索引)
? ?返回值:返回索引對應的字符
? ? console.log(str.charAt(0));
?? ?
? ?charCodeAt
? ?功能:返回索引對應的字符的asc碼值,
? ? 參數(shù):charCodeAt(索引)
? 返回值:返回索引對應的字符的asc碼值,
? ?console.log(str.charCodeAt(0));
?? ?
? ? length
? ?console.log(str.length);
字符串API
? ?charAt
? ?charCodeAt
? ? length
?? ?
?? ?// fromCharCode
?? ?// 功能:將asc碼值轉換為字符,
?? ?// 參數(shù):fromCharCode(asc1,[asc2...]);
?? ?// 返回值:轉換后的字符串,
?? ?//說明:通過string調(diào)用
?? ?// var str = String.fromCharCode(97,98);
?? ?// console.log(str);
?? ?
?? ?//indexOf
?? ?//功能:查找子串首次出現(xiàn)的位置
?? ?//參數(shù):indexOf(子串)
?? ?//返回值:找到返回子串的首字母下標,找不到返回-1
?? ?
?? ?//lastIndexOf
?? ?//功能:從末尾查找子串首次出現(xiàn)的位置
?? ?//參數(shù):lastIndexOf(子串)
?? ?//返回值:找到返回子串的首字母下標,找不到返回-1
?? ?// var str = "helloworld";
?? ?// console.log(str.indexOf("o"));
?? ?// console.log(str.lastIndexOf("o"));
?? ?
?? ?// replace
?? ?// 功能:字符串替換,
?? ?// 參數(shù):replace(被替換字符串,替換字符串);
?? ?// 返回值:新字符串
?? ?// var str = "zhurongrui de ge bi shi zhurongrui";
?? ?// str = str.replace("zhurongrui","老王");
?? ?// console.log(str);
?? ?// slice
?? ?// 功能:字符串截取,
?? ?// 參數(shù):slice(起始位置,結束位置),左閉右開,
?? ?// 返回值:截取的子串
????substring:幾乎和slice一毛一樣
?? ?console.log(str.substring(2,5));
區(qū)別slice支持負數(shù)
?? ?console.log(str.slice(-5,-2));
split
?? ?// 功能:字符串分割函數(shù),將分割的字符串轉換為數(shù)組,
?? ?// 參數(shù):split(分隔符)
?? ?// 返回值:數(shù)組,
總結
以上是生活随笔為你收集整理的选择排序、json对象、indexof、回调函数、ES5新增遍历函数、字符串定义、asc码表、字符串API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: k2p 老毛子纯净版固件
- 下一篇: ip软件在生活中器到哪些作用呢?