代码之家  ›  专栏  ›  技术社区  ›  RogerHN

计算数组中具有特定名称的条目数,并保存以供以后使用

  •  0
  • RogerHN  · 技术社区  · 7 年前

    我正试图找到一种方法来计算此数组中每种类型的条目数:

    var types = [
      "loc_67249556",
      "loc_52558678",
      "loc_62658330",
      "gra_59669755",
      "gra_59289309"
    ]
    

    我指的“类型”是每个条目的前三个字母。 我使用for循环数组并提取3个首字母:

    for (var i = 0; i < types.length; k++) {
        var tp= types[k].split('_');
        tp = tp[0];
    }
    

    这将输出3个第一个字母,但我如何计算它们并保存它们,以便以后使用?

    类似于一个新数组,其中包含类型以及它们在第一个数组中出现的次数,如下所示:

    var types_count = [
      {
        type: "loc",
        qnt: 3
      },
      {
        type: "gra",
        qnt: 2
      }
    ]
    

    4 回复  |  直到 7 年前
        1
  •  5
  •   CertainPerformance    7 年前

    使用 reduce

    const types = [
      "loc_67249556",
      "loc_52558678",
      "loc_62658330",
      "gra_59669755",
      "gra_59289309"
    ];
    
    const typeCount = types.reduce((a, str) => {
      const type = str.slice(0, 3);
      if (!a[type]) a[type] = { type, count: 0 };
      a[type].count++;
      return a;
    }, {})
    console.log(Object.values(typeCount));
        2
  •  3
  •   Code Maniac    7 年前

    你可以在你的帮助下试试这个 reduce()

    var types = [
      "loc_67249556",
      "loc_52558678",
      "loc_62658330",
      "gra_59669755",
      "gra_59289309"
    ];
    
    let op = Object.values(types.reduce((op,curr)=>{
      let temp = curr.substr(0,3);
      if( op[temp] ){
        op[temp]['count']++;
      } else {
        op[temp] ={
          'type': temp,
          'count': 1
        }
      }
      return op;
    },{}))
    console.log(op);
        3
  •  1
  •   Mister Jojo Łukasz Daniel Mastalerz    7 年前

    如果类型_count与前一作品的某些值存在(或不存在):

    var types = [
      "loc_67249556",
      "loc_52558678",
      "loc_62658330",
      "gra_59669755",
      "gra_59289309"
    ];
    
    var types_count = [{type:'bef', qnt: 5}]; // values from a previous op
    // or var types_count = []; // empty
    
    
    // update types_count with new values and increase previous count
    types.forEach(function(elm) {
      let
        tp = elm.split('_')[0],
        ix = types_count.findIndex(e=>e.type===tp)
      ;
      if (ix<0) { types_count.push( {  type: tp,  qnt: 1 } )
      } else    { types_count[ix].qnt++; }
    });
    
    // because there is [sorting] in your tags:
    types_count.sort((a,b) => (a.type > b.type) ? 1 : ((b.type > a.type) ? -1 : 0));
    
    console.log( types_count );
        4
  •  0
  •   Ahmed Ghoniem    7 年前

    check this

        var getcount = function (types) {
            var result = [];
            types.map(obj => {
                var key = obj.split('_')[0];
                var tempArr = result.filter(item => item.key == key);
                if (tempArr.length > 0) {
                    tempArr[0].count = tempArr[0].count + 1;
                } else {
                    var tempitem = {
                        count: 1,
                        key: key
                    };
                    result.push(tempitem);
                }
            });
            return result;
        }
    
        var types = [
            "loc_67249556",
            "loc_52558678",
            "loc_62658330",
            "gra_59669755",
            "gra_59289309"
        ]
        //to get result call it 
        var newArr = getcount(types);
        console.log(newArr);