【问题标题】:Grouping same values from JavaScript array [duplicate]从JavaScript数组中分组相同的值[重复]
【发布时间】:2019-05-05 22:44:12
【问题描述】:
let array = ['a', 'a', 'a', 'b', 'b', 'c'];

我想对数组中的双精度值进行分组,得到这样的结果:

结果: ['3a, 2b, c']
(或类似的东西)

有什么想法吗?

【问题讨论】:

  • 你真的想要一个数组中的单个字符串作为结果吗?你试过什么?
  • @NinaScholz 就是这样

标签: javascript arrays dictionary filter reduce


【解决方案1】:

我建议使用字典来跟踪数组中的重复值。

var dictionary = {};
for(var i = 0;i < array.length;i++){
   var value = array[i];
   if(dictionary[value] === undefined){
      dictionary[value] = 0;
   }
   dictionary[value] = dictionary[value] + 1;

}
console.log(dictionary)

【讨论】:

    【解决方案2】:

    您可以使用.reduce().map() 方法:

    let array = ['a', 'a', 'a', 'b', 'b', 'c'];
    
    let result = Object.entries(
        array.reduce((r, c) => (r[c] = (r[c] || 0) + 1, r) , {})
    ).map(([k, v]) => v == 1 ? k : v + k);
    
    console.log(result);

    如果您需要特定顺序的物品,也可以使用Map

    let array = ['a', 'a', 'a', 'b', 'b', 'c'];
    
    let result = (((arr, map) => {
        arr.forEach(s => map.set(s, (map.get(s) || 0) + 1));
        return [...map.entries()].map(([k, v]) => v == 1 ? k : v + k);
    })(array, new Map()));
    
    console.log(result);

    【讨论】:

      【解决方案3】:

      您可以使用Array.reduce() 生成具有键值(数组值和计数)对的对象,然后使用Array.map() 生成使用Object.keys() 的数组。

      let array = ['a', 'a', 'a', 'b', 'b', 'c'];
      
      let countObj = array.reduce((acc, val) => (acc[val] = acc[val] ? acc[val] + 1 : 1, acc), {});
      
      console.log(countObj);
      
      let countArr = Object.keys(countObj).map(key => '' + countObj[key] + key);
      
      console.log(countArr);

      【讨论】:

        【解决方案4】:
        function count() {
        array_elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"];
        array_elements.sort();
        var current_elements = null;
        var count= 0;
        for (var i = 0; i < array_elements.length; i++) {
            if (array_elements[i] != current_elements) {
                if (cnt > 0) {
                    console.log(current_elements+count);
                }
                current_elements= array_elements[i];
                count= 1;
            } else {
                count++;
            }
        }
        if (cnt > 0) {
            console.log(current_elements+count);
        }
        

        }

        【讨论】:

          【解决方案5】:

          对于“类似的东西”,为什么不只使用一个将字母映射到出现的对象。然后,您可以简单地使用点表示法来获取分配给每个键的属性值。

          这里我用过reduce

          const arr = ['a', 'a', 'a', 'b', 'b', 'c'];
          
          // `reduce` over the arr
          const obj = arr.reduce((acc, c) => {
          
            // If the object (acc) we passed in doesn't have
            // a key assigned to the letter in the current
            // iteration (c) add it, set it to 0, then
            // add one, otherwise, if there is a key, add one
            acc[c] = (acc[c] || 0) + 1;
          
            // Return the object for the next iteration
            return acc;
          
          // Pass in an initial object 
          }, {});
          
          console.log(obj);
          
          // Grab the value of `a` property
          console.log(obj.a);

          【讨论】:

            【解决方案6】:

            简单的run-length encoding (RLE) 函数的单循环方法。

            let array = ['a', 'a', 'a', 'b', 'b', 'c'],
                result = array.reduce(
                    (r, c, i, a) => r.concat(c === a[i - 1]
                        ? ((+r.pop().slice(0, -1) || 1) + 1) + c
                        : c),
                    []
                );
            
            console.log(result);

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2017-11-12
              • 1970-01-01
              • 2022-01-09
              • 1970-01-01
              • 2016-03-03
              • 2016-09-15
              相关资源
              最近更新 更多