使用元素的值及其在 JavaScript 中重复的次数创建一个子数组数组

Create an array of sub-arrays with the value of an element and the number of times it has repeated in JavaScript

我正在尝试获取一个数组(例如,一个年份数组),然后创建一个新的子数组数组,它​​首先告诉原始数组中的唯一元素,其次,如何重复了很多次。

例如,假设我从一组数字开始 [1999, 1999, 2000, 2005, 2005, 2005, 2015]

我希望我的函数 return 像 [[1999, 2], [2000, 1], [2005, 3], [2015, 1] ]
这样的数组 因为1999年重复了两次,2000年没有重复,2005年重复了三次,等等

我可以成功创建一个删除重复项的新数组,但是在创建子数组时我遇到了一些奇怪的行为。

编辑

这是我自己的错误解决方案,以防有人指出我做错了什么。

var populateYearsList = function(yearsDuplicate){

var uniqueYears = [];
for (var i=0; i < yearsDuplicate.length; i++){
 if(uniqueYears.indexOf(yearsDuplicate[i]) == -1){
  uniqueYears.push(yearsDuplicate[i])
} else {
  console.log("duplicate found") };
}
 console.log (uniqueYears)
};

我 运行 遇到了尝试将 uniqueYears.push(yearsDuplicate[i]) 更改为 uniqueYears.push([ yearsDuplicate[i], 1 ]) 然后尝试将我的 console.log 替换为某种递增计数器的问题。

我会这样做:

var input = [1999, 1999, 2000, 2005, 2005, 2005, 2015];

function numberOfOccurancesArray (input) {
    var result = {};

    for (var i = 0; i < input.length; i++) {

        var currentYear = input[i];

        // if there is an element with the name of the currentYear
        if (result[currentYear]) {

            // increace its prop `count` with 1
            result[currentYear].count += 1;

        } else {

            // if not present, create it
            result[currentYear] = {
                year: currentYear,
                count: 1
            }
        }
    }

    return result;
}

这里是示例代码:JsFiddle

就这么简单:

var input = [1999, 1999, 2000, 2005, 2005, 2005, 2015];

var uniq = [];
input.forEach(function(n){
    if (uniq.indexOf(n)<0) 
        uniq.push(n);
});

var output = uniq.map(function(n){
    return [n, input.filter(function(m){ return m == n }).length]
});

Quick explanation how it works

Given the input array, map its unique version to the new array with this mapping:

element ---> [ element, the number of occurrences in the original array ]

编辑说明:: 修复了之前可能引入重复元素的解决方案。

var years = [1999, 1999, 2000, 2005, 2005, 2005, 2015];

var occurences = [];

for(var i = 0; i < years.length; i++ ) {
    var year = years[i];
    if(typeof occurences[year] === 'undefined') {
        occurences[year] = 1;
    } else {
        occurences[year]++;   
    }
}

occurences = occurences.map(function(occurences, year) {
    return [year, occurences];
});

console.log(occurences);

编辑:更快的解决方案

    var results = [];
    var uniq = [];
    var counts = [];
    var i = 0;

    for (i; i < years.length; i++) {
        var year = years[i];
        var indexOf = uniq.indexOf(year);
        if (indexOf < 0) {
            uniq.push(year);
            counts[uniq.length - 1] = 1;
        } else {
            counts[indexOf] += 1;
        }
    }

    i = 0;

    for (i; i < uniq.length; i++) {
        results.push([uniq[i], counts[i]]);
    }

    return results;

如果您想使用函数式范例,将其简化为字典,然后将键映射到元组。

var yearToCounts = input.reduce(function(counts, year) {
    if (year in counts) {
        counts[year]++;
    } else {
        counts[year] = 1;
    }
    return counts;
}, {});

return Object.keys(yearToCounts).map(function(year) {
    return [year, yearToCounts[year]];
});