使用数组的循环计算发生次数

calculate occurrence using loop of an array

本文关键字:计算 循环 数组      更新时间:2023-09-26
['a','a','b','a','s','g','h','t']

我有上面的数组,使用循环,如何知道所有字母出现的次数?比如a出现了多少次?

预期的结果如下:

a - 3
b - 5
s - ..

一种方法是:

// Declare an array, in which you will store the distinct elements
// of your initial array.
var characters = [];
// Declare an array, in which you will store the times you find 
// each distinct element in your initial array.
// The counter in the 1st position is associated with the character 
// in the 1st position of characters.
var counters = [];
// Your initial array.
array = ['a','a','b','a','s','g','h','t'];
// Loop through the items of your array
for(var i=0, len=array.length; i<len; i++){
    var indexOfChar = characters.indexOf(array[i]);
    // If the current item is not included in the characters
    // push it there and push the corresponding counter to the
    // counters.
    if(indexOfChar===-1){
        characters.push(array[i]);
        counters.push(1);
    }else{ // The current item is found. So just increment the counter.
        counters[indexOfChar] += 1;
    }
}
// Print the results
for(var i=0, len=characters.length; i<len; i++){
    document.write(characters[i]+'-'+counters[i]+'<br/>');
}

var characters = [];
var counters = [];
array = ['a','a','b','a','s','g','h','t'];
for(var i=0, len=array.length; i<len; i++){
    var indexOfChar = characters.indexOf(array[i]);
    if(indexOfChar===-1){
        characters.push(array[i]);
        counters.push(1);
    }else{ 
        counters[indexOfChar] += 1;
    }
}
for(var i=0, len=characters.length; i<len; i++){
   document.write(characters[i]+'-'+counters[i]+'<br/>');
}

这可以在forEach中完成吗?这伤了我的眼睛

当然有,但这不是我的第一个想法。

var array = ['a','a','b','a','s','g','h','t'];
var dict = {}; 
array.forEach(function (char) { dict[char] = dict[char] ? dict[char] + 1 : 1; }); 
for(var key in dict){
    if(dict.hasOwnProperty(key)){
        document.write(key+'-'+dict[key]+'<br/>');
    }
}

var myarray = ['a','a','b','a','s','g','h','t'];
var countarray = [];
var Item = function(letter){
  this.letter = letter;
  this.count = 0;
}
for(var i=0;i<myarray.length;++i){
  countarray[myarray[i]] = countarray[myarray[i]] || new Item(myarray[i]);
  countarray[myarray].count++;
}

您可以创建包含特定数组中存在的元素数量的新对象。

请参考下面的示例,该示例创建了一个对象,其中包含每个字符出现的所有详细信息-

elements_arr = ['a','a','b','a','s','g','h','t'];
//below statement creates object that will store details of character as a key and occurrence as a value .
occured_details = {};
for(i=0;i<elements_arr.length;i++)
{
    //if object having particular element the increase by one.
    if(occured_details.hasOwnProperty(elements_arr[i]))
    {
        occured_details[elements_arr[i]] += 1;
    }
    else//if object doesn't have character as a key then create new key and assign 1 occurrence to the key.
    {
       occured_details[elements_arr[i]] = 1;
    }
}
//
jQuery.each(occured_details, function(key, element) {
    alert('key: ' + key + ''n' + 'value: ' + element);
}); 

要查看示例,请使用此链接-http://jsfiddle.net/ks9tt953/

试试下面的代码:

   var freq = [];
   var ar = ['a', 'a', 'b', 'a', 's', 'g', 'h', 't'];
    for (x = 0; x < ar.length; x++) {
        freq[ar[x]] = 0;
    }/*Initialize*/
    for (y = 0; y < ar.length; y++) {
        freq[ar[y]] += 1;
    }/*Operate*/
    for (i in freq) {
        console.log(i + "-" + freq[i]);
        /*This will display your desired output*/
    }/*Output*/