zoukankan      html  css  js  c++  java
  • js面试题,代码实现,持续更新中

    怎么判断两个对象相等?

    JSON.stringify(obj) === JSON.stringify(obj)

    实现数组去重?

    let arr = [1,1,'true','true',true,true,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
    • 使用ES6的Set方法
    console.log(Array.from(new Set(arr)) console.log([...new Set(arr)])

    不能去重对象

    • hasOwnProperty
    function unique(arr) {
            var obj = {};
            return arr.filter(function(item, index, arr){
                return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)
            })
        }
        console.log(unique(arr))

    综合考虑是最好的方法

    • indexOf
    function unique(arr) {
        if (!Array.isArray(arr)) {
            console.log('type error!')
            return
        }
        var array = [];
        for (var i = 0; i < arr.length; i++) {
            if (array .indexOf(arr[i]) === -1) {
                array .push(arr[i])
            }
        }
        return array;
    }
    console.log(unique(arr))
    • includes
    function unique(arr) {
        if (!Array.isArray(arr)) {
            console.log('type error!')
            return
        }
        var array =[];
        for(var i = 0; i < arr.length; i++) {
                if( !array.includes( arr[i]) ) {//includes 检测数组是否有某个值
                        array.push(arr[i]);
                    }
        }
        return array
    }
    console.log(unique(arr))
    • 双层for循环

    function unique(arr){            
        for(var i=0; i < arr.length; i++){
            for(var j=i+1; j < arr.length; j++){
                if(arr[i]==arr[j]){      
                    arr.splice(j,1);
                    j--;
                }
            }
        }
    return arr;
    }

    实现深拷贝的方法?

    let obj = {a: undefined, b: 1.7976931348623157E+10308, c: -1.7976931348623157E+10308, d: new Date(), e: new RegExp('\w+'), f: function() {console.log(1)}}
    • 第一种:JSON.parse(JSON.stringify())

      弊端如下

      • 如果obj里面有时间对象,时间将只是字符串的形式。而不是时间对象;
      • 如果obj里有RegExp、Error对象,则序列化的结果将只得到空对象
      • 如果obj里有函数,undefined,则序列化的结果会把函数或 undefined丢失;
      • 如果obj里有NaN、Infinity(正无穷大)和-Infinity(负无穷大),则序列化的结果会变成null
      • 如果obj中的对象是有构造函数生成的,会丢弃对象的constructor;
    • 第二种:手写迭代

      function deepClone(obj){
          let newObj = Array.isArray(obj)?[]:{}
          if(obj && typeof obj === 'object'){
              for(let key in obj){
                  if(obj.hasOwnProperty(key){
                      newObj[key] = (obj && typeof obj[key] === 'object') ? deepClone(obj[key]) : obj[key]
                  }
              }
          }
          return newObj
      }
      console.log(deepClone(obj))

    实现冒泡排序算法?

    let array = [5, 4, 3, 2, 1];
    let temp = 0;
    for (let i = 0; i <array.length; i++){
      for (let j = 0; j <array.length - i; j++){
        if (array[j] > array[j + 1]){
          temp = array[j + 1];
          array[j + 1] = array[j];
          array[j] = temp;
        }
      }
    }

    实现降维数组?

    let arr = [[1,2],[3,4]]
    function Jw(arr){
      return Array.prototype.concat.apply([], obj)  
    }
    console.log(Jw(arr)) // [1,2,3,4]

    实现取数组的最大值

    • Math.max.apply(null, [213, 4, 43, 141])
    • Math.max(...[213, 4, 43, 141])

    如何阻止冒泡事件

    function stopBubble(e){
        if(e && e.stopPropagation){
            // 非IE浏览器
            e.stopPropagation();
        }else{ 
            //IE浏览器
            window.event.cancelBubble=true;
        }
    }

    如何阻止浏览器默认事件

    function stopDefault(e){ 
        //标准浏览器
        if(e && e.preventDefault){ 
            e.preventDefault(); 
        } 
        //个别IE
        else{ 
            window.event.returnValue=fale;
            return false;
        } 
    }

    获取非行间样式

    function getCss(curEle,attr){
        var val = null;
        try{
                val = window.getComputedStyle(curEle,null)[attr];
        }catch(e){
                val = curEle.currentStyle[attr];
        }
        return val;
    }
    getCss(div,'width')

     

  • 相关阅读:
    VS2008 编译出错 fatal error C1859: unexpected precompiled header error, simply rerunning the compiler might fix this problem
    解析XML出错,无法创建DOMDocument对象
    strncpy, strncpy_s
    Mongodb: Sort operation used more than the maximum 33554432 bytes of RAM
    node-cache
    【Boost】boost::string_algo详解2——find相关函数
    Compiler Error: Function call with parameters that may be unsafe
    fopen和fopen_s用法的比较
    fopen_s遇到的一个问题
    Data type conversion in MongoDB
  • 原文地址:https://www.cnblogs.com/naturl/p/15457272.html
Copyright © 2011-2022 走看看