Code Monkey home page Code Monkey logo

javascript-tips's People

Contributors

zenglinan avatar

Watchers

 avatar

javascript-tips's Issues

求数组的交集、并集

并集

两个数组

思路:合并两数组之后去重即可,关键在于数组去重。

Set

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 && for of

for in

for in 可以遍历对象以及原型上的可枚举属性,每次取到的是 key

for of

for of 遍历的是可迭代对象,每次取到的是 value

可迭代对象

可迭代对象:包含 [Symbol.iterator] 属性,这个属性实现了迭代接口

内置的可迭代对象:String、Array、arguments、Map、Set

用 reduce 写高阶函数

需求:

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'))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.