• js 统计数组中元素的个数


    var all = "02 06 11 12 19 29 09 10 12 19 22 29 08 11 13 19 28 31 07 08 09 15 22 27 10 18 19 29 32 33 05 07 10 23 28 29 01 07 10 16 22 33 11 12 13 16 23 25 07 09 18 22 23 29 03 06 13 14 19 28 03 05 06 13 20 22 01 05 06 16 25 30 01 05 11 20 22 24 03 08 14 20 24 26 14 18 21 25 28 29 03 05 14 25 26 30 01 12 16 20 22 24 03 07 14 23 25 27 05 07 18 19 22 24 01 02 04 15 17 22 01 02 04 15 17 22 01 04 08 09 14 15 01 03 06 19 21 29 06 09 10 14 28 30 03 11 12 16 21 23 02 12 13 18 25 27 03 06 11 26 30 32 01 11 12 18 25 27 11 15 20 21 26 33 09 12 20 24 28 31 06 08 13 15 22 33 11 12 13 19 26 28 03 10 21 23 27 33 01 08 17 20 21 22 05 10 17 23 26 32 05 09 10 12 17 19 13 14 20 25 27 31 02 07 08 09 17 29 02 20 21 28 31 33 14 18 19 26 30 31 01 14 16 17 20 31 07 18 24 29 31 33 01 08 11 26 28 31 05 09 13 15 18 26 07 11 12 13 18 19 06 10 23 25 26 29 02 05 07 09 11 27 06 14 19 20 21 23 05 08 15 20 27 30 04 07 11 14 29 32 03 07 20 21 25 31 01 19 25 26 27 33 02 06 12 17 25 28 01 03 06 19 21 29";
    var arr = all.split(" ");
    var b = {}; // 用来存储次数的对象
    // arr 源数组
    // fn 用来对数据进行加工,reduce函数本身并没有进行次数累计功能
    // initial 存储统计次数用的函数
    reduce(arr, function(s, s1, idx) {
    s[s1] = (s[s1] + 1) || 1;
    return s;
    }, b);

    function reduce(arr, fn, initial) {
    return(function reduceOne(index, value) {
    if(index > arr.length - 1) return value;
    return reduceOne(index + 1, fn(value, arr[index], index, arr));
    })(0, initial);
    }

  • 相关阅读:
    [Cerc2013]Magical GCD
    UVA 1393 Highways
    UVA 10214 Trees in a Wood
    [SDOI2010]大陆争霸
    Oracle逻辑读详解
    DBA_2PC_PENDING (转)
    oracle autotrace使用
    升级oracle 9i到10g
    VMware 虚拟机中添加新硬盘的方法(转载)
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql (转)
  • 原文地址:https://www.cnblogs.com/lgjc/p/8579750.html
Copyright © 2020-2023  润新知