使用数组的循环计算发生次数
calculate occurrence using loop of an array
['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*/
相关文章:
- JavaScript循环无法正确计算/显示结果
- Javascript循环计算
- 循环 HTML 表并将相同的项目及其计算的金额添加到另一个表中
- Do/while 循环不计算掷骰子次数或与骰子对齐
- jQuery根据选定的下拉值循环进行计算
- 在 JavaScript 中,在循环中创建函数的具体原因是什么,可能会浪费计算时间
- j查询循环计算
- Javascript:如何在第 2 和第 3 语句中使用三元运算符计算 for 循环
- 使用基于年龄的循环进行计算
- Jquery in 循环执行一些记录计算
- 为..in - 被迭代的对象在进入循环之前是否只计算一次
- 距离计算在循环剂量不起作用
- 如何计算jQuery中每个循环的总时间
- while-循环完成问题和最终值计算
- 在嵌套在 for 循环 (JavaScript/jQuery) 中的 switch 语句中计算 jQuery 数组的元素
- 使用条件语句和 while 循环 JavaScript 计算 2 个日期之间的闰年
- 计算循环执行的次数
- 如何对 for 循环正在处理的数组进行延迟计算
- 如何在 JavaScript 中使用 while 循环计算平均值
- 我能绕过这个计算循环吗?