1. 扁平化嵌套数组/flat实现

描述:将嵌套多层的数组展开平铺成只有一层的数组。

let array = [1, [1, 2, 3], [1, [2, {}]] ]
handle(array) // [1, 1, 2, 3, 1, 2, {}]
复制代码

方法一

const handle = array => JSON.parse(`[${JSON.stringify(array).replace(/\[|]/g,'')}]`)
handle(array)   // [ 1, 1, 2, 3, 1, 2, {} ]
复制代码

知识点JSON.parse()/JSON.stringify()String.prototype.replace()

方法二

const handle = array => array.reduce((accumulator, currentValue) => accumulator.concat(Array.isArray(currentValue) ? handle(currentValue): currentValue), [])
handle(array)   // [ 1, 1, 2, 3, 1, 2, {} ]
复制代码

知识点Array.prototype.reduce()Array.prototype.concat()

方法三

const handle = array => {while(array.some(item => Array.isArray(item))) {array = [].concat(...array)}return array
}
handle(array)   // [ 1, 1, 2, 3, 1, 2, {} ]
复制代码

知识点whileArray.prototype.some()展开语法(Spread syntax)

其它方法:......

2. 数组去重

描述:将数组中重复的元素过滤掉。

let array = [1, 2, 1, '3', '3', 0 , 1]
handle(array)   // [1, 2, '3', 0]
复制代码

方法一

const handle = array => [...new Set(array)]
handle(array)   // [ 1, 2, '3', 0 ]
复制代码

知识点:Set

方法二

const handle = array => array.reduce((accumulator, currentValue) => {!accumulator.includes(currentValue) && accumulator.push(currentValue)return accumulator
}, [])
handle(array)   // [ 1, 2, '3', 0 ]
复制代码

知识点:Array.prototype.includes()

方法三

const handle = array => {let map = new Map()return array.filter(item => map.has(item) ? false : map.set(item))
}
handle(array)   // [ 1, 2, '3', 0 ]
复制代码

知识点MapArray.prototype.filter()

其它方法:......

3. 模拟bind实现

Function.prototype.bind = function () {let self = this, args = Array.from(arguments), context = args.shift();return function () {return self.apply(context, args.concat(...arguments))};
};
复制代码

知识点apply、call、bind

4. 模拟New实现

const handle = function() {let fn = Array.prototype.shift.call(arguments)let obj = Object.create(fn.prototype)let o = fn.apply(obj, arguments)return typeof o === 'object' ? o : obj;
}
复制代码

知识点Object.create()

5. 格式化数字

const num = 123456789;
const handle = num => String(num).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
handle(num) // 123,456,789
复制代码

知识点正则表达式String.prototype.replace()

6. 回文判断

const num = 123456654321;
const str = 'abababababab';
const handle = params => {let str_1 = String(params).replace(/[^0-9A-Za-z]/g, '').toLowerCase();let str_2 = str_1.split('').reverse().join();return str_1 === str_2 ? true : false
}
handle(num) // true
handle(str) // false
复制代码

知识点String.prototype.split()Array.prototype.join()

7. 函数节流

定时器

const handle = (fn, interval) => {let timeId = null;return function() {if (!timeId) {timeId = setTimeout(() => {fn.apply(this, arguments)timeId = null}, interval)}}
}
复制代码

知识点window.setTimeout

时间戳

const handle = (fn, interval) => {let lastTime = 0return function () {let now = Date.now();if (now - lastTime > interval) {fn.apply(this, arguments)lastTime = now}}
}
复制代码

8. 函数防抖

const handle = (fn, delay) => {let timeId;return function() {if (timeId) clearTimeout(timeId)timeId = setTimeout(() => {fn.apply(this, arguments)}, delay)}
}
复制代码

函数节流、函数防抖区别:函数节流和函数防抖较容易混淆,可以这么比喻,对于函数节流,门外有人频繁敲门,但是门卫按固定时间来决定是否开门。对于函数防抖,门外有人频繁敲门,门卫按最后一次敲门来决定是否开门。

知识点window.clearTimeout

9. 发布订阅模式

class Pubsub {constructor() {this.handles = {}}subscribe(type, handle) {if (!this.handles[type]) {this.handles[type] = []}this.handles[type].push(handle)}unsubscribe(type, handle) {let pos = this.handles[type].indexOf(handle)if (!handle) {this.handles.length = 0} else {~pos && this.handles[type].splice(pos, 1)}}publish() {let type = Array.prototype.shift.call(arguments)this.handles[type].forEach(handle => {handle.apply(this, arguments)})}
}const pub = new Pubsub()
pub.subscribe('a', function() {console.log('a', ...arguments)})
pub.publish('a', 1, 2, 3)
// a 1 2 3
复制代码

前端笔试之手写代码(一)相关推荐

  1. 前端面试高频手写代码题

    前端面试高频手写代码题 一.实现一个解析URL参数的方法 方法一:String和Array的相关API 方法二: Web API 提供的 URL 方法三:正则表达式+string.replace方法 ...

  2. 前端面试:手写代码JS实现字符串反转

    前端萌新面试:手写代码JS实现字符串反转 前言 因为做前年小红书的前端校招面试题,发现出现好几道关于字符串对象和数组对象的题目,说难不难,但突然要写的话一时想不起来,这不想着做个小总结. 首先明白字符 ...

  3. 前端面试之手写代码篇

    手写代码 1.手写instanceof方法 2.手写new操作符 3.手写Promise.all() 4.手写防抖函数 5.手写节流函数 6.手写call.apply函数 7.手写bind函数 8.封 ...

  4. 2020年前端面试之JS手写代码题合集

    2020年前端面试之JS手写代码题合集 预计会有上千道题,后续慢慢补! 1.  写一个把字符串大小写切换的方法 function caseConvert(str){return str.replace ...

  5. 应对笔试手写代码,如何准备深度优先算法 广度优先算法?

    应对笔试手写代码,如何准备深度优先算法 & 广度优先算法? 1. 什么是深度优先算法?什么又是广度优先算法? 2. 广度优先算法使用场景 3. 广度优先算法模板 4. 深度优先算法使用场景 5 ...

  6. 2021-最新Web前端经典面试试题及答案-史上最全前端面试题(含答案)---手写代码篇

    ★★★ 手写代码:实现forEach map filter reduce ★★★ 手写实现一个简易的 Vue Reactive ★★★ 手写代码,监测数组变化,并返回数组长度 ★★★ 手写原生继承,并 ...

  7. 手写代码(笔试面试真题)

    ★★★ 手写代码:实现forEach map filter reduce ★★★ 手写实现一个简易的 Vue Reactive ★★★ 手写代码,监测数组变化,并返回数组长度 ★★★ 手写原生继承,并 ...

  8. IOS纯手写代码支持旋屏

    2019独角兽企业重金招聘Python工程师标准>>> 不用ib纯手写代码实现旋屏效果,xcode4.6.3,今天试了一下,可以做到,但是代码量会增加,基本思路是:在 - (void ...

  9. 揭秘 ClownFish 比手写代码还快的原因

    说明:本文的第一版由于反对人数较多(推荐/反对数量是:23 / 17), 我在8月20日删除了博文内容,只留下一段简单的内容. 既然分享技术也引来这么多的反对,那我就不分享了. 如果希望知道我的优化方 ...

最新文章

  1. NOIP2011 提高组 Day1
  2. 显卡在电脑什么位置_显卡是什么?电脑显卡有什么用?——《作用篇》
  3. 让 fork 出来的 Github 仓库从远端仓库拖取最新的修改
  4. dnf公共频道服务器不稳定已从初始化状态,DNF公共频道跨区列表 组队连不上必看...
  5. P3291-[SCOI2016]妖怪【凸壳】
  6. js的动态加载、缓存、更新以及复用(四)
  7. 小心了!一大波存储厂商术语正在靠近
  8. 调用新浪微博显示用户信息
  9. c语言基础总结代码练习(小白也看得懂) 模拟银行业务系统
  10. 无盘服务器chkdsk *: /f)修复命令,让你的电脑运行更快点 使用CHKDSK/F磁盘修复命令...
  11. python爬取淘宝数据
  12. 五笔打字怎么学,负基础
  13. Error starting child
  14. Python简单实现人脸识别检测, 对某平台美女主播照片进行评分排名
  15. 手机免流量,还会是天方夜谭吗?
  16. Unity Metaverse(二)、Mixamo Animator 混合树与动画融合
  17. 谁会使用IEC61499
  18. 机器学习实战——决策树(二)
  19. ra3录像重播工具_设置DO-RA小工具的网络销售渠道
  20. 思岚科技亮相2017电子博览会 倍受瞩目

热门文章

  1. Web安全之拖放劫持
  2. 物联网通信协议——比较-MQTT、 DDS、 AMQP、XMPP、 JMS、 REST、 CoAP
  3. Mac OS开发—Xcode给Mac应用添加编辑快捷键(剪切 复制 粘贴 全选 删除 撤销 重做)功能
  4. Linux系统编程:lseek扩展文件大小失败原因分析
  5. 查找算法:折半查找算法实现及分析
  6. md5加密+盐方式一
  7. Win7如何关闭 打开文件-安全警告
  8. 移动端微信公众号开发中问题记录及解决方案
  9. 两岸三地在线编程学习网站大全
  10. IOS的一些文件操作。(沙箱) 在Documents目录下创建文件