How to count duplicate value in an array in javascript

前端 未结 28 1314
后悔当初
后悔当初 2020-11-22 06:07

Currently, I got an array like that:

var uniqueCount = Array();

After a few steps, my array looks like that:

uniqueCount =          


        
相关标签:
28条回答
  • 2020-11-22 06:13
    uniqueCount = ["a","b","a","c","b","a","d","b","c","f","g","h","h","h","e","a"];
    var count = {};
    uniqueCount.forEach((i) => { count[i] = ++count[i]|| 1});
    console.log(count);
    
    0 讨论(0)
  • 2020-11-22 06:15

    Nobody responding seems to be using the Map() built-in for this, which tends to be my go-to combined with Array.prototype.reduce():

    const data = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
    const result = data.reduce((a, c) => a.set(c, (a.get(c) || 0) + 1), new Map());
    console.log(...result);

    N.b., you'll have to polyfill Map() if wanting to use it in older browsers.

    0 讨论(0)
  • 2020-11-22 06:15

    simplified sheet.js answare

    var counts = {};
    var aarr=['a','b','a'];
    aarr.forEach(x=>counts[x]=(counts[x] || 0)+1 );
    console.log(counts)

    0 讨论(0)
  • 2020-11-22 06:15

    By using array.map we can reduce the loop, see this on jsfiddle

    function Check(){
        var arr = Array.prototype.slice.call(arguments);
        var result = [];
        for(i=0; i< arr.length; i++){
            var duplicate = 0;
            var val = arr[i];
            arr.map(function(x){
                if(val === x) duplicate++;
            })
            result.push(duplicate>= 2);
        }
        return result;
    }
    

    To Test:

    var test = new Check(1,2,1,4,1);
    console.log(test);
    
    0 讨论(0)
  • 2020-11-22 06:15

    Usage:

    wrap.common.getUniqueDataCount(, columnName);
    

    CODE:

    function getUniqueDataCount(objArr, propName) {
            var data = [];
            objArr.forEach(function (d, index) {
                if (d[propName]) {
                    data.push(d[propName]);
                }
            });
    
            var uniqueList = [...new Set(data)];
    
            var dataSet = {};
            for (var i=0; i < uniqueList.length; i++) {
                dataSet[uniqueList[i]] = data.filter(x => x == uniqueList[i]).length;
            }
            
            return dataSet;
        }
    

    Snippet

    var data= [
              {a:'you',b:'b',c:'c',d:'c'},
              {a: 'you', b: 'b', c: 'c', d:'c'},
              {a: 'them', b: 'b', c: 'c', d:'c'},
              {a: 'them', b: 'b', c: 'c', d:'c'},
              {a: 'okay', b: 'b', c: 'c', d:'c'},
              {a: 'okay', b: 'b', c: 'c', d:'c'},
              ];
              
      console.log(getUniqueDataCount(data, 'a'));       
      
      function getUniqueDataCount(objArr, propName) {
            var data = [];
            objArr.forEach(function (d, index) {
                if (d[propName]) {
                    data.push(d[propName]);
                }
            });
    
            var uniqueList = [...new Set(data)];
    
            var dataSet = {};
            for (var i=0; i < uniqueList.length; i++) {
                dataSet[uniqueList[i]] = data.filter(x => x == uniqueList[i]).length;
            }
    
            return dataSet;
        }

    0 讨论(0)
  • 2020-11-22 06:17
    var arr = ['a','d','r','a','a','f','d'];  
    
    //call function and pass your array, function will return an object with array values as keys and their count as the key values.
    duplicatesArr(arr);
    
    function duplicatesArr(arr){
        var obj = {}
        for(var i = 0; i < arr.length; i++){
            obj[arr[i]] = [];
            for(var x = 0; x < arr.length; x++){
                (arr[i] == arr[x]) ? obj[arr[i]].push(x) : '';
            }
            obj[arr[i]] = obj[arr[i]].length;
        }
    
        console.log(obj);
        return obj;
    }
    
    0 讨论(0)
提交回复
热议问题