字符串有哪些常用API,至少10个
数组API,至少15个
手写数组的批处理APIdemo各一个
arr.forEach(item=>console.log(item))
const doubles = arr.map(item=>2*item)
const evens = arr.filter(item=>item%2===0)
const allEven = arr.every(item=>item%2===0)
const hasEven = arr.some(item=>item%2===0)
const sum = arr.reduce((pv,cv)=>pv+cv)
正则相关的5个API各写一个demo
const hasRegSubstr = reg.test(str)
const groupsArr = reg.exec(str)
const substrs = str.match(reg)
const firstSubstrIndex = str.search(reg)
const newStr = str.replace(reg,sub=>newSub)
fn.bind/call/apply demo各一个
function fn(a,b){
console.log(this,a,b)
}
const thisArg = {name:"jack"}
let newFn = fn.bind(thisArg,"我是入参a","我是入参b")
newFn()
newFn = fn.bind(thisArg)
newFn("我是入参a","我是入参b")
fn.call(thisArg,"我是入参a","我是入参b")
fn.apply(thisArg,["我是入参a","我是入参b"])
Set与Map
- ES6的新增数据结构
- Set用于基础数据的去重,一些核心API如下:
cosnt mySet = new Set([3,1,4,1,5,9,2,6,5,3,5,8])
const norepeatArr = [...mySet]
mySet.add(value)
mySet.has(value)
mySet.size
- Map用于键值对的便捷操作与查询,底层就是Object
const contacts = new Map()
contacts.set('Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"})
contacts.has('Jessie')
contacts.get('Hilary')
contacts.delete('Raymond')
contracts.size