解構(gòu)賦值
*特別有用棺聊,尤其是ajax賦值
let [a,b,c] = [12,4,5]
console.log(a,b,c) //12,4,5
注意:左右兩邊結(jié)構(gòu)要保持一直
json:
let {name,age,job} = {
name:'hany',
age:18,
job:'程序員'
}
字符串模板
``字符串模板--優(yōu)點:可以隨意換行 ${變量名}
let name = '韓梅梅'
let age = 19
let str = 這個人叫${name},今年${age}歲
console.log(str) //這個人叫韓梅梅姚炕,今年19歲
字符串的相關操作
1.字符串查找
str.indexOf(要找的東西)休雌,返回索引位置扣泊,沒有找到返回-1
str.includes(要找的東西),返回值true或者false
2.判斷瀏覽器
console.log(navigator.userAgent.includes('Chrome')) true/false
3.字符串是否以誰開頭
str.startsWith(檢測東西)
4.字符串是否以誰結(jié)尾
str.endsWith(檢測東西)
5.重復字符串
str.repeat(次數(shù))
6.填充字符串
let str = 'apple'
let padstr = 'xxx'
let endstr = 'xxx'
str.padStart(整個字符串長度,填充東西)
console.log(str.padStart(str.length+padstr.length,padstr))
str.endStart(整個字符串長度,填充東西)
console.log(str.endStart(str.length+endstr.length,endstr))
擴展運算符,Rest運算符(...方法)
...: [1,2,3,4]->...[1,2,3,4]->1,2,3,4
...: 1,2,3,4,5->...1,2,3,4,5->[1,2,3,4,5]
剩余參數(shù)用法:必須放到最后
function show(a,b,...c){
console.log(a,b) //1,2
console.log(c) //[3,4,5]
}
show(1,2,3,4,5)
數(shù)組相關方法
Array.from()
作用:把類數(shù)組(獲取一組元素慧邮,Arguments...)對象轉(zhuǎn)成數(shù)組
觀點:只要具備length就可以使用
let array = {
0: 'apple',
1: 'orange',
2: 'mango',
3: ['people1','people2','people3'],
'length': 4
}
let arr = Array.from(array )
console.log(arr) // ['apple','orange','mango',['people1','people2','people3']]
Array.of()
作用:把一組值晴叨,轉(zhuǎn)成數(shù)組
let arr = Array.of('apple','banana','orange')
console.log(arr) // ["apple", "banana", "orange"]
arr.find()
作用:找出第一個符合條件的數(shù)組成員,如果沒找到就返回undefind
let arr = [1,9,20,60,100,61,101,300]
let newarr = arr.find((val, index, arr) => {
return val > 200
})
console.log(newarr) //300
arr.findIndex()
作用:查找符合條件數(shù)組成員下標麻捻,沒有找到返回-1
let arr = [1,9,20,60,100,61,101,300]
let newarr = arr.findIndex((val,index,arr)=>{
return val>200
})
console.log(newarr) // 7
arr.fill()
作用:填充 arr.fill(填充的東西纲仍,開始位置呀袱,結(jié)束位置)
let arr = new Array(10)
arr.fill('默認',1,3)
console.log(arr) //[empty, "默認", "默認", empty × 7]
arr.includes()
作用:arr.includes(要找的東西)查找數(shù)組中是否有某個值,返回值true或者false
arr.forEach()
代替普通for循環(huán)
let arr = ['one','two','three','four','five']
arr.forEach((val,index,arr)=>{
console.log(val,index,arr)
//val是值 index下標 arr數(shù)組
})
arr.map()
非常有用,做數(shù)據(jù)交互"映射"郑叠,正常情況下夜赵,需要配合return,返回一個新數(shù)組乡革,若是沒有return寇僧,相當于forEach
let arr = [
{title:'標題1',read:50,hot:100},
{title:'標題2',read:60,hot:200},
{title:'標題3',read:70,hot:300},
{title:'標題4',read:80,hot:400},
]
let newarr = arr.map((val,index,arr)=>{
let newjson = {}
newjson.head = 新${val.title}
newjson.click = val.read + 100
newjson.heat = val.hot * 2
return newjson
})
console.log(newarr)
//[{title:'新標題1',read:150,hot:200},{title:'新標題2',read:160,hot:400},{title:'新標題3',read:170,hot:600},{title:'新標題4',read:180,hot:800}]
注意:平時只要用map,一定要有return
arr.filter()
過濾沸版,過濾一些不合格的"元素",如果回調(diào)函數(shù)返回true视粮,則保留下來
let arr = [
{title:'標題1',read:50,hot:100},
{title:'標題2',read:60,hot:200},
{title:'標題3',read:70,hot:300},
{title:'標題4',read:80,hot:400},
]
let newarr = arr.filter((val,index,arr)=>{
return val.hot == 100
})
console.log(newarr)//[{title:'標題1',read:50,hot:100}]
arr.some()
類似查找细办,數(shù)組里面某一個元素符合條件,返回true
let arr = ['one','two','three','four','five']
let newarr = arr.some((val,index,arr)=>{
return val == 'three'
})
console.log(newarr)//true
arr.every()
數(shù)組里面所有元素都要符合條件馒铃,才返回true
let arr = [1,3,5,7,9]
let newarr = arr.every((val,index,arr)=>{
return val%2 == 1
})
console.log(newarr)//true
箭頭函數(shù)
let show = function(){}
let show = ()=>{}
注意:
1.this的問題蟹腾,定義函數(shù)所在的對象,不在是運行所在的對象
2.箭頭函數(shù)里面沒有arguments,用'...'
3.箭頭函數(shù)不能當構(gòu)造函數(shù)
對象相關用法
Object.is()
作用:用來比較兩個值是否相等
Object.is('a','a') //true
Object.is(NaN,NaN) //true
Object.is(+0,-0) //false
Object.assign()
作用:用來合并對象,合并參數(shù)
let 新對象 = Object.assign(目標對象,source1,source2,source3...)
let json1 = {a:1}
let json2 = {b:2}
let json3 = {c:3}
let newObj = Object.assign({},json1,json2,json3) //{a:1,b:2,c:3}