前端笔试题(1)

1.对给定数组实现倒排


// 方法一
const numberArray = [ 3, 6, 2, 4, 1, 5 ]
numberArray.reverse()

//方法二
const numberArray = [ 3, 6, 2, 4, 1, 5 ]
const newArr = []
for (let i = 0; i < numberArray.length; i++) {
  newArr.unshift(numberArray[i])
}

2.对给定数组实现降序和升序


//实现降序
const numberArray = [ 3, 6, 2, 4, 1, 5 ]
numberArray.sort((a, b) => {
  return b - a
})

//实现降升序
// 方法一
const numberArray = [ 3, 6, 2, 4, 1, 5 ]
numberArray.sort((a, b) => {
  return a - b
})

// 方法二 通过冒泡排序实现数组升序
const numberArray = [ 3, 6, 2, 4, 1, 5 ]
function BubbleSort (arr) {
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[i] > arr[j]) {
        const tmp = arr[j]
        arr[j] = arr[i]
        current = tmp
      }
    }
  }
  return arr
}
BubbleSort(numberArray)

// 方法三 通过插入排序实现数组排序
const numberArray = [ 3, 6, 2, 4, 1, 5 ]

function insertSort (arr) {
  for (let i = 1; i < arr.length; i++) {
    for (let j = i - 1; j >= 0; j--) {
      if (arr[j + 1] < arr[j]) {
        const temp = arr[j]
        arr[j] = arr[j + 1]
        arr[j + 1] = temp
      } else if (arr[j + 1] >= arr[j]) {
        break
      }
    }
  }
  return arr
}
insertSort(numberArray)

// 方法四 通过计数排序实现数组排序
const numberArray = [ 3, 6, 2, 4, 1, 5 ]
function CountSort (arr) {
  const obj = {}
  for (let i = 0; i < arr.length; i++) {
    if (!obj[arr[i]]) {
      obj[arr[i]] = 1
    } else {
      obj[arr[i]]++
    }
  }
  let index = 0
  // 遍历对象属性名,按顺序放回覆盖原数组
  for (const key in obj) {
    while (obj[key] > 0) {
      arr[index] = Number(key)
      obj[key]--
      index++
    }
  }
  return arr
}
console.log(CountSort(numberArray))

3.JavaScript 中如何对一个对象进行深度 clone?

方法1


function clone (obj) {
  // 判断数据类型是不是对象
  if (typeof obj === 'object') {
    // 因为数组也属于对象,细分传入的值是不是数组
    if (obj instanceof Array) {
      const result = []
      for (let i = 0; i < obj.length; i++) {
        // 递归调用
        result[i] = clone(obj[i])
      }
      return result
    } else {
      const result = {}
      for (const i in obj) {
        // 递归调用
        result[i] = clone(obj[i])
      }
      return result
    }
  } else {
    return obj
  }
}
const obj1 = [ 12, { a: 11, b: 22 }, 5 ]
const obj2 = clone(obj1)
obj2[1].a += 5
console.log(obj1, obj2)

方法2


 // 把处理过数据的存起来
  let cachedMap = new Map();
  const deepClone = (obj) => {
    // 是不是基础数据类型
    if (typeof obj !== 'object' || !obj) return obj;
    // 判断是否已经处理过
    if (cachedMap.has(obj)) return cachedMap.get(obj)
    let constructor = obj.constructor
    let tmp, params;
    // 是不是Data和正则
    if (obj instanceof RegExp || obj instanceof Date) {
      params = obj;
    }
    tmp = new constructor(params)
    // 把处理过数据的存起来
    cachedMap.set(obj, tmp)
    // 是不是Map/Set/object
    if (obj instanceof Map) {
      for (let [key, value] of obj) {
        tmp.set(deepClone(key),deepClone(value))
      }
    } else if (obj instanceof Set) {
      for (let value of obj) {
        tmp.add(deepClone(value))
      }
    } else {
      for (let key in obj) {
        tmp[key] = deepClone(obj[key])
      }
    }
  }
posted @ 2023-04-04 14:10  FAIGEL  阅读(28)  评论(0)    收藏  举报