js

對象排重

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();
複製代碼
相關文章
相關標籤/搜索