Javascript面试超高频手写60题:1-10(持续更新中)

本文详细解析了如何实现数组扁平化,包括一维数组操作的`merge`函数和指定层数的扁平化方法,以及JavaScript中常用的map、filter、reduce等高阶函数。涵盖了数组处理、函数式编程和数组操作技巧。
摘要由CSDN通过智能技术生成

1.数组扁平化

面试中数组扁平化通常有两种版本,90%的公司都要求实现扁平化成一维数组,而10%的公司会要求扁平化到指定层

扁平化到一维

arr=[1,2,[3],[4,[5]]]的结果[1,2,3,4,5]

const merge = (arr) => {
  let res = []
  const main = (node) => {
    node.forEach(element => {
      if(!Array.isArray(element)) res.push(element)
      else main(element)
    });
  }
  main(arr)
  return res
}
扁平化指定层数

arr=[1,2,[3],[4,[5]]]的扁平两次结果[1,2,3,4,[5]]

const merge = (arr,count) => {
  let res = []
  const main = (node,num) => {
    if(num===1) {
      res.push(...node)
      return 
    }
    node.forEach(element => {
      if(!Array.isArray(element)) res.push(element)
      else main(element,num-1)
    });
  }
  main(arr,count)
  return res
}

2.map

Array.prototype.map = function(callback) {
  let len = this.length
  let res = []
  for(let i=0;i<len;i++) {
    res.push(callback(this[i],i))
  }
  return res
}

3.filter

Array.prototype.filter = function(callback) {
  let len = this.length
  let res = []
  for(let i=0;i<len;i++) {
    if(callback(this[i],i))
      res.push(this[i])
  }
  return res
}

4.reduce

 Array.prototype.reduce = function(callback,begin) {
  let len = this.length
  for(let i=0;i<len;i++) {
    if(!begin) {
      begin = callback(this[i],this[i+1],i+1,this)
      i++
    } else {
      begin = callback(begin,this[i],i,this)
    }
  }
  return begin
}

5.使用reduce实现map(字节真题)

 Array.prototype.map = function(callback) {
  return this.reduce((begin,val)=>{
    begin.push(callback(val))
    return begin
  },[])
}

6.forEach

 Array.prototype.forEach = function(callback) {
  let len =  this.length
  for(let i=0;i<len;i++) 
    callback(this[i],i)
}

7.instanceof

function myInstanceof (obj,Target) {
  let __proto = obj.__proto__
  while(__proto) {
    if(__proto === Target.prototype) return true
    __proto = __proto.__proto__
  }
  return false
}
//测试代码
function People (name) {
  this.name = name
}
let p = new People("dzp")
console.log(myInstanceof(p,People))//true

8.apply

 Function.prototype.apply = function(o,args=[]) {
  o = o || window
  let key = Symbol()
  o[key] = this
  let res = o[key](...args)
  delete o[key]
  return res
}
//测试
function fn () {
  console.log(this.age)
}
let obj = {age:22}
fn.apply(obj)

9.call

 Function.prototype.call = function(o,...args) {
  o = o || window
  let key = Symbol()
  o[key] = this
  let res = o[key](...args)
  delete o[key]
  return res
}
//测试
function fn () {
  console.log(this.age)
}
let obj = {age:22}
fn.call(obj)

10.bind

Function.prototype.bind = function(o,...args) {
  o = o || window
  let key = Symbol()
  o[key] = this
  return ()=>{
    return o[key](...args)
  }
}
//测试
function fn () {
  console.log(this.age)
}
let obj = {age:22}
fn.bind(obj)()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值