I need to calculate the occurence of the name in an array.
var eachAuthorData = ["bob","joke","hello","stack","stack","ok","joke","bob"];
I can do this by using a for loop,and get a result like this
{bob: 2, joke: 2, hello: 1, stack: 2, ok: 1}
but the result can only be access using
counts["bob"]
not only do I need to know the occrence of each name. but also need to know how many diffent name in the array eachAuthorData how do I locate bob in the eachAuthorData I only need to use the name once
If you start with an array:
var eachAuthorData = ["bob","joke","hello","stack","stack","ok","joke","bob"];
and you want to know how many of each name there is in the array and how many unique strings there are, you can do it like this:
var counts = {}, i, item, uniques = 0;
for (i = 0; i < eachAuthorData.length; i++) {
item = eachAuthorData[i];
if (!counts.hasOwnProperty(item)) {
counts[item] = 1;
++uniques;
} else {
++counts[item];
}
}
This will generate an output in counts
like this:
{bob: 2, joke: 2, hello: 1, stack: 2, ok: 1}
And, since you asked a few other questions in comments, I added the variable uniques
which gives you the total unique string count.
And, you would access any individual count like this:
var cnt = counts["bob"];
Or, if the desired key is in a variable named key
, you would use:
var cnt = counts[key];
If you want to iterate all the counts of all the unique strings, you can do that like this:
for (var item in counts) {
// item is the key
// counts[item] is the count
console.log("counts[" + item + "] = " + counts[item]);
}
Working demo: http://jsfiddle.net/jfriend00/eJyAg/
If you want a list of the keys in the counts object (eg the unique strings), you can use:
var keys = Object.keys(counts);
Object.keys()
requires IE9 or greater or there's a polyfill here if you want interoperability with older versions of IE.
I think you should have:
var eachAuthorData = ["bob","joke","hello","stack","stack","ok","joke","bob"];
do this to count:
var toMatch, i, count;
toMatch = "bob";
count = 0;
for( i=0 ; i<eachAuthorData.length ; ++i ){
if( eachAuthorData[i] == toMatch ){
++count;
}
}
console.log("The element "+ toMatch +" is counted "+ count +" times!");
Try this one
var tempObject = new Object();
function count() {
array_elements = ["bob","joke","hello","stack","stack","ok","joke","bob"];
array_elements.sort();
var current = null;
var cnt = 0;
for (var i = 0; i < array_elements.length; i++) {
if (array_elements[i] != current) {
if (cnt > 0) {
tempObject[current]=cnt;
console.log(current + ' comes --> ' + cnt + ' times<br>');
}
current = array_elements[i];
cnt = 1;
} else {
cnt++;
}
}
if (cnt > 0) {
console.log(current + ' comes --> ' + cnt + ' times');
tempObject[current]=cnt;
}
}
count();
console.log(JSON.stringify(tempObject));
Presumably you meant an array like:
var data = ["bob","joke","hello","stack","stack","ok","joke","bob"];
Since others have gone the obvious route, I'll post something a little more robust that accounts for the possibility that the data array has elided members (ie is sparse):
function countMembers(dataArray) {
for (var i=0, iLen=dataArray.length, o={}, item; i<iLen; i++) {
item = dataArray[i];
if (dataArray.hasOwnProperty(i)) {
if (o.hasOwnProperty(item)) {
++o[item];
} else {
o[item] = 1;
}
}
}
return o;
}
Given:
["bob","joke",,,,"hello","stack","stack","ok","joke","bob"];
others will include a member undefined: 3
, but the above wont.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.