zenglinan / javascript-tips Goto Github PK
View Code? Open in Web Editor NEWJavascript 的一些实用技巧
Javascript 的一些实用技巧
思路:合并两数组之后去重即可,关键在于数组去重。
function union(arr1, arr2){
return [...new Set([...arr1,arr2])]
}
function union(arr1, arr2){
const arr = [...arr1, ...arr2]
const tmp = {}
return arr.filter(item => {
const key = Object.prototype.toString.call(item) + JSON.stringify(item) // 类型 + 值 作为 key
return tmp.hasOwnProperty(key) ? false : (tmp[key] = true)
})
}
多个数组的情况稍微复杂一点,需要判断输入的是否是数组,遍历参数将多个数组合并,然后去重,下面采用了 Set 方法,如果需要更好的控制权,可以用对象存储方法去重。
function union(){
let result = []
for(let i = 0, len = arguments.length; i < len; i ++){
if(!Array.isArray(arguments[i])){
throw Error('请传入数组参数')
}
result = result.concat(arguments[i])
}
return [...new Set(result)]
}
思路:去重 arr1,然后遍历 arr1,只保留 arr2 includes 的元素
function diff(arr1, arr2){
arr1 = [...new Set(arr1)]
return arr1.filter(item => {
return arr2.includes(item)
})
}
思路:类似两个数组,先去重 arr1, 然后遍历 arr1, 每个元素都需要包含在剩余的数组中,否则不保留该元素。
function diff(){
const arr1 = [...new Set(arguments[0])]
return arr1.filter(item => {
let flag = true // 是否是需要的交集元素
for(let i = 1, len = arguments.length; i < len; i++){
if(!arguments[i].includes(item)){ // 只要一个数组不包含 说明不是交集元素
flag = false
break
}
}
return flag
})
}
for in
可以遍历对象以及原型上的可枚举属性,每次取到的是 key
for of
遍历的是可迭代对象,每次取到的是 value
可迭代对象:包含 [Symbol.iterator]
属性,这个属性实现了迭代接口
内置的可迭代对象:String、Array、arguments、Map、Set
需求:
将 compose(add$, len, sum)('c','d')
按照 add$(len(sum('c','d')))
的顺序执行
function len(arg){
return arg.length
}
function sum(){
const len = arguments.length
let r = ''
for(let i = 0; i < len; i ++){
r += arguments[i]
}
return r
}
function add$(arg){
return `*${arg}`
}
function compose(...fns){
return function(...arg){
const fn = fns.pop() // 取出第一个要执行的函数
return fns.reduceRight((prev, cur) => {
return cur(prev) // 关键
}, fn(...arg))
}
}
console.log(compose(add$, len, sum)('c','d'))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.