[关闭]
@jeffjade 2017-06-01T10:56:14.000000Z 字数 1119 阅读 986

JS 之妙用

javascript


如何求出两个数组的交集、差集和并集?

  1. // 求交集(a ∩ b)两种方法
  2. let intersection = a.filter(v => b.includes(v))
  3. let intersectionSet = Array.from(new Set([...a].filter(x => b.includes(x))))
  4. // 求差集(a - b)两种方法
  5. let difference = a.concat(b).filter(v => !a.includes(v) || !b.includes(v))
  6. let differenceSet = Array.from(new Set([...a].filter(x => !b.includes(x))))
  7. // 求并集(a + b)两种方法
  8. let union a.concat(b).filter(function(item, index, array) {
  9. return array.indexOf(item) === index;
  10. })
  11. let unionSet = Array.from(new Set([...a, ...b]))
  1. // let defMaps = {}
  2. // let executive = require('./executive').default
  3. // let finance = require('./finance').default
  4. // let design = require('./design').default
  5. // let market = require('./market').default
  6. // let legal = require('./legal').default
  7. // let it = require('./it').default
  8. // let hr = require('./hr').default
  9. // Object.assign(
  10. // defMaps,
  11. // executive,
  12. // finance,
  13. // design,
  14. // market,
  15. // legal,
  16. // hr,
  17. // it
  18. // )
  19. // export const $maps = defMaps
  20. const files = require.context('.', true, /\.js$/)
  21. const configs = {}
  22. const moudles = []
  23. files.keys().forEach((key) => {
  24. if (key === './index.js') return
  25. moudles[key.replace(/(\.\/|\.js)/g, '')] = files(key)
  26. })
  27. for (let key in moudles) {
  28. Object.assign(configs, moudles[key].default)
  29. }
  30. export const $maps = configs
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注