js

334 阅读2分钟

对象排重

    var a = [{ id: 1 }, { id: 2 }, { id: 1 }, { id: 2 }, { id: 3 }, { id: 1 }, { id: 4 }]
    var b = []
    a.forEach((item, index) => {
    var flag = true
    b.forEach((it, i) => {
    if (a[index].id == b[i].id) {
    flag = false;
    };
    })
    if (flag) {
    b.push(a[index]);
    };

    })
    console.log(b)

对象排序

    var arr = [
    {name:'zopp',age:0},
    {name:'gpp',age:18},
    {name:'yjj',age:8}
  ];

function compare(property){
    return function(a,b){
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2;
    }
}
console.log(arr.sort(compare('age')))

深浅拷贝

var data = {
    list: [1, 2, 3, 4],
    fn: function () {
        alert(56789)
    },
    listdate: [
        {
            str: "8888"
        }, {
            arr: [{
                txt: " al al al "
            }]
        }
    ]
}

function copy(data) {
    var obj;
    if (Object.prototype.toString.call(data) === '[object Object]') {
        obj = {}
        for (let key in data) {
            if (typeof data[key] == "object") {
                obj[key] = copy(data[key])
            } else {
                obj[key] = data[key]
            }
        }
    } else if (Object.prototype.toString.call(data) === '[object Array]') {
        obj = []
        data.forEach((items) => {
            if (typeof items == "object") {
                obj.push(copy(items))
            } else {
                obj.push(items)
            }
        })
    } else {
        obj = data
    }
    return obj
}

var newDA = copy(data)
newDA.listdate[1].arr[0].txt = "jjjjjjj"
console.log(data.listdate[1].arr[0].txt)
console.log(newDA.listdate[1].arr[0].txt)

冒泡排序

   //思路:先比较一轮一次,然后用for循环比较一轮多次,然后再加for循环比较多轮多次
    //从大到小排序
    var array=[10,20,9,8,79,65,100];
    //比较轮数
    for ( var i=0;i<array.length-1;i++){
        //每轮比较次数,次数=长度-1-此时的轮数
        for (var j=0;j<array.length-1-i;j++) {
            if (array[j] > array[j + 1]) {
                var temp = array[i];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            } //end if
        }//end for 次数
    } //end for 轮数
    console.log(array);

字符串中出现次数最多的一个字符


let testStr = 'asdasddsfdsfadsfdghdadsdfdgdasd';
        function getMax(str) {
            let obj = {};
            for(let i in str) {
                if(obj[str[i]]) {
                    obj[str[i]]++;
                }else{
                    obj[str[i]] = 1;
                }
            }
            let keys = Object.keys(obj); // 获取对象中所有key的值返回数组
            let values = Object.values(obj); // 获取所有value返回数组
                let maxVal = Math.max(...values);// Math.max可以找出传入参数的最大值,如:Math.max(1,2);这里可使用es6中的解构,
                也可以使用Math.max.apply(Math,values)可认为是apply(Math.max, arr)
                然后,arr是一个参数列表,对于max方法,其参数是若干个数,即Math.max(a, b, c, d, ...)
            console.log(keys[values.indexOf(maxVal)],maxVal);
        }
        getMax(testStr);

// obj值:{a: 5, s: 7, d: 12, f: 4, g: 2,  h: 1, s: 7,}

函数防抖(debounce)

const _.debounce = (func, wait) => {
  let timer;

  return () => {
    clearTimeout(timer);
    timer = setTimeout(func, wait);
  };
};

函数节流(throttle)

const _.throttle = (func, wait) => {
  let timer;

  return () => {
    if (timer) {
      return;
    }

    timer = setTimeout(() => {
      func();
      timer = null;
    }, wait);
  };
};

二叉树

   //初始化二叉树对象
    function Node(data,left,right) {
        this.data = data;
        this.left = left;
        this.right = right;
    }
    //定义插入对象
    function BST(){
        this.root = null;
        this.insert = insert;
        this.show = ()=>{
           console.log(this.root);
        }
    }
    function insert(data) {
        //实例化Node对象
        let n = new Node(data,null,null);
        //如果不存在节点,则此节点是根节点
        if(this.root == null){
            this.root = n;
        }else{
            //存在根节点时,定义current白能量等于根节点
            let current = this.root;
            let parent;
            while(current){
                parent = current;
                //当插入的值小于根节点的值时,将值作为左节点插入
                if(data<current.data) {
                    current = current.left;
                    if(current == null) {
                        parent.left = n;
                        break;
                    }
                }else{
                    current = current.right;
                    if(current == null){
                        parent.right = n;
                        break;
                    }
                }
            } 
        }
    }
    const bst = new BST();
    bst.insert(13);
    bst.insert(21);
    bst.insert(15);
    bst.insert(29);
    bst.insert(3);
    bst.insert(55);
    bst.show();