How can you caount the number of occurrences of elements in an array? For
example
var arr1 =
[1,5,1,1,5,2,4,5,2,3,2,4,2,4,2,3,2,3,2,4,2,3,2,4,2, 5,1,4,2,4,2,3,2,4,1];
how can I count how many 1s, 2s, 3s, etc.?
Despite the example, I never actually know what elements (and how many) the
array holds, which means, that the following is NOT a solution for me:
for( var i =0; i <arr1.length; i++) {
if (arr1[i] ==1) cnt1++;
if (arr1[i] ==2) cnt2++;
etc.
}
Thanks,
Rafal 9 110648
Rafal Konopka wrote: How can you caount the number of occurrences of elements in an array? For example
var arr1 = [1,5,1,1,5,2,4,5,2,3,2,4,2,4,2,3,2,3,2,4,2,3,2,4,2, 5,1,4,2,4,2,3,2,4,1];
how can I count how many 1s, 2s, 3s, etc.? http://www.php.net/manual/en/functio...unt-values.php
JW
"Rafal Konopka" wrote How can you caount the number of occurrences of elements in an array? For example
var arr1 = [1,5,1,1,5,2,4,5,2,3,2,4,2,4,2,3,2,3,2,4,2,3,2,4,2, 5,1,4,2,4,2,3,2,4,1];
how can I count how many 1s, 2s, 3s, etc.?
Despite the example, I never actually know what elements (and how many)
the array holds
Several approaches exist. This one retuns an object with the array values as
keys and the number of times they occur as values:
function countvalues(a) {
var b = {}, i = a.length, j;
while( i-- ) {
j = b[a[i]];
b[a[i]] = j ? j+1 : 1;
}
return b;
}
var arr=['you','you','you','me','me','them','them','others'];
alert( countvalues(arr)['you'] ); // 3
hth
--
Ivo http://4umi.com/
Rafal Konopka wrote: How can you caount the number of occurrences of elements in an array? For example
var arr1 = [1,5,1,1,5,2,4,5,2,3,2,4,2,4,2,3,2,3,2,4,2,3,2,4,2, 5,1,4,2,4,2,3,2,4,1];
how can I count how many 1s, 2s, 3s, etc.?
The following might be useful:
var arr1 =
[1,5,1,1,5,2,4,5,2,3,2,4,2,4,2,3,2,3,2,4,2,3,2,4,2, 5,1,4,2,4,2,3,2,4,1];
var count = [];
var msg = '';
for (var i = 0; i < arr1.length; i++) {
if (count[arr1[i]]) {
count[arr1[i]] += 1;
} else {
count[arr1[i]] = 1;
}
}
for (i in count) msg += i + ' occurres ' + count[i] + ' times\n';
alert(msg);
JW
Thank,
I'll give it a whirl and see what happens
Rafal
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42***********************@news.euronet.nl... var arr1 = [1,5,1,1,5,2,4,5,2,3,2,4,2,4,2,3,2,3,2,4,2,3,2,4,2, 5,1,4,2,4,2,3,2,4,1]; var count = []; var msg = '';
for (var i = 0; i < arr1.length; i++) { if (count[arr1[i]]) { count[arr1[i]] += 1; } else { count[arr1[i]] = 1; } }
for (i in count) msg += i + ' occurres ' + count[i] + ' times\n'; alert(msg);
JW
Ivo, It's a nice and crisp solution, but I cannot pass a parameter to the
function as I never know what elements the array holds.
Essentiall, I was wondering if the newer versions of javascript had
something similar to (and oh-so-useful) Perl's
$count{$val}++;
vivifying the hash by creating the key (the unknown element) and value (its
count).
Rafak
"Ivo" <no@thank.you> wrote in message
news:42***********************@news.wanadoo.nl... Several approaches exist. This one retuns an object with the array values
as keys and the number of times they occur as values:
function countvalues(a) { var b = {}, i = a.length, j; while( i-- ) { j = b[a[i]]; b[a[i]] = j ? j+1 : 1; } return b; }
var arr=['you','you','you','me','me','them','them','others']; alert( countvalues(arr)['you'] ); // 3
hth -- Ivo http://4umi.com/
Rafal Konopka wrote: Ivo, It's a nice and crisp solution, but I cannot pass a parameter
to the function as I never know what elements the array holds.
Essentiall, I was wondering if the newer versions of javascript had something similar to (and oh-so-useful) Perl's
$count{$val}++;
vivifying the hash by creating the key (the unknown element) and
value (its count).
Rafak
"Ivo" <no@thank.you> wrote in message news:42***********************@news.wanadoo.nl... Several approaches exist. This one retuns an object with the array
values as keys and the number of times they occur as values:
function countvalues(a) { var b = {}, i = a.length, j; while( i-- ) { j = b[a[i]]; b[a[i]] = j ? j+1 : 1; } return b; }
var arr=['you','you','you','me','me','them','them','others']; alert( countvalues(arr)['you'] ); // 3
hth -- Ivo http://4umi.com/
Maybe...
Array.prototype.countvalues = function(k)
{
var b = {}, i = this.length, j;
while( i-- )
{
j = b[this[i]];
b[this[i]] = j ? j+1 : 1;
}
return b[k];
}
var arr=['you','you','you','me','m*e','them','them','others '];
alert( arr.countvalues('you') ) // 3
This won't distinguish between string/numeric primitives ('4' / 4)
however; don't think you can hash with these as JS objects treat
string/numeric integer subscripts similarly.
Rafal Konopka wrote: Ivo, It's a nice and crisp solution, but I cannot pass a parameter to the function as I never know what elements the array holds.
If you just want to get a reduced array with unique elements and
the count of occurrences, then Ivo's solution actually does
exactly that. You just need to modify the output:
<script type="text/javascript">
function countvalues(a) {
var b = {}, i = a.length, j;
while( i-- ) {
j = b[a[i]];
b[a[i]] = j ? j+1 : 1;
}
return b; // an object of element:count arrays
}
var arr=['you','you','me','you',
'me','them','them',
'others','you'];
var a = countvalues(arr);
var msg='';
// To see how many times each element appears
for (elem in a){
msg += '\n' + elem + ' : ' + a[elem];
}
alert(msg);
// To see how many times a particular element appears
alert( countvalues(arr)['you'] ); // 4
</script>
--
Rob
Thanks to all who replied to my question (counting elements of an array).
It was in fact very educational.
Rafal This discussion thread is closed Replies have been disabled for this discussion. Similar topics
9 posts
views
Thread by Nathan Rose |
last post: by
|
5 posts
views
Thread by effendi |
last post: by
|
6 posts
views
Thread by Herrcho |
last post: by
|
3 posts
views
Thread by thomasp |
last post: by
|
4 posts
views
Thread by ralf321 |
last post: by
|
2 posts
views
Thread by Jon |
last post: by
|
3 posts
views
Thread by Henrik Goldman |
last post: by
| | | | | | | | | | | | |