• js 案例


    1、复杂数组去重

    let arr = [
        { name: 'atom',age: '15'},
        { name: 'aili',age:'16'},
        { name:'atom',age:'18'},
        { name: 'aili', age: '16'}
    ]
    
    function removeRepeatArray(arr) {
        let unique = {};
        arr.forEach(key => {
                unique[JSON.stringify(key)] = key;
        });
        arr = Object.keys(unique).map(data => JSON.parse(data));
        return arr;
    }
    console.log(removeRepeatArray(arr))
    // [
    //    { name: 'atom',age: '15'},
    //    { name: 'aili',age:'16'},
    //    { name:'atom',age:'18'},
    //]
    View Code

    2、1+2+3....+100之和

    let i=0,item,n=100,sum;
    sum = (1+ n) *n / 2;
    console.log(sum)  //5050
    View Code

     3、数组重复个数

    let arr = ['apple','orange', 'pear', 'apple', 'orange']
     function getWordCnt () {
         return arr.reduce(function(prev, next){
             prev[next] = (prev[next] + 1) || 1
             return prev;
         },{})
     }
     console.log(getWordCnt()) //{apple: 2, orange: 2, pear: 1}
    View Code

     4、数据类型检测

     function classOf(obj) {
         if (obj === null) return "Null";
         if (obj === undefined) return 'Undefined';
         return Object.prototype.toString.call(obj).slice(8, -1);
     }
    View Code
  • 相关阅读:
    js闭包
    python切片 []取值操作符
    python with语句
    python鸭子类型(duck type)
    python编码规范
    python @property使用详解
    python __slots__使用详解
    Python面向对象编程
    ifconfig命令详解
    5、Cocos2dx 3.0游戏开发找小三之測试例子简单介绍及小结
  • 原文地址:https://www.cnblogs.com/vhen/p/7808041.html
Copyright © 2020-2023  润新知