Reputation: 19
This is my object how can i get all the N count and all the S count. At least how can i access that N and Y variables from this array.
DoctorD:"N", DoctorE:"N", DoctorF:"N", DoctorG:"N", DoctorH:"N", DoctorI:"N", DoctorJ:"N", DoctorK:"N", DoctorL:"N", DoctorM:"N", DoctorN:"N", DoctorO:"N", DoctorP:"N", DoctorQ:"N", DoctorR:"N", DoctorS:"N", DoctorT:"N", DoctorU:"N", DoctorV:"Y", DoctorW:"N", DoctorX:"N", DoctorY:"N", DoctorZ:"N", DoctorAA:"N", DoctorAB:"N", DoctorAC:"N", DoctorAE:"N", DoctorAF:"N", DoctorAG:"N", DoctorAH:"N", DoctorAI:"N", DoctorAJ:"N", DoctorAK:"N", DoctorAL:"N", DoctorAM:"N", DoctorAN:"Y", DoctorAO:"Y", DoctorAP:"Y", DoctorA:"Y", DoctorAQ:"Y", DoctorAR:"Y", DoctorAS:"Y", DoctorAT:"N", DoctorAU:"Y", DoctorAV:"Y", DoctorAW:"Y", DoctorAX:"Y", DoctorB:"Y", DoctorAY:"Y", DoctorAZ:"Y", DoctorBA:"Y", DoctorBB:"Y", DoctorBC:"Y", DoctorC:"Y", DoctorBD:"Y", DoctorBE:"Y", DoctorBF:"Y"
in javascript.Any help would be appreciated.Is this possible? or else any other solution to do this?
Upvotes: 1
Views: 76
Reputation: 19645
Might be a bit overkill for your purposes, but you asks for counts. This will return an object containing the count of each value in found in the original object. You might want to do more type checking. For example, ensure the value of the object is something meaningful or of an expected value. Its a bit meaningless to count {'DoctorA': ['N','S'], 'DoctorB': ['N','S']};
using the function below.
Here is a working example: http://jsfiddle.net/Fjfm8/
More on the For .. In method :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
/**
* count the values from a set of key value pairs and count the occurrences of each value
* @param {object} input - an object containing key value pairs
* @throws {error} TypeError - if <tt>input</tt> is not an object
* @return {object} counts - an object keying the number of times each value occurs
*/
var value_count = function (input) {
if (Object.prototype.toString.call(input) === '[object Object]') {
var counts, key, val;
console.log(input);
counts = {};
for (key in input) {
val = input[key];
if (input.hasOwnProperty(key)) {
if (counts.hasOwnProperty(val)) {
counts[val] += 1;
} else {
counts[val] = 1;
}
}
}
return counts;
} else {
throw new TypeError('expected input to be an Object');
}
};
//input
var input = {
DoctorD: "N",
DoctorE: "N",
DoctorF: "N",
DoctorG: "N",
DoctorH: "N",
DoctorI: "N",
DoctorJ: "N",
DoctorK: "N",
DoctorL: "N",
DoctorM: "N",
DoctorN: "N",
DoctorO: "N",
DoctorP: "N",
DoctorQ: "N",
DoctorR: "N",
DoctorS: "N",
DoctorT: "N",
DoctorU: "N",
DoctorV: "Y",
DoctorW: "N",
DoctorX: "N",
DoctorY: "N",
DoctorZ: "N",
DoctorAA: "N",
DoctorAB: "N",
DoctorAC: "N",
DoctorAE: "N",
DoctorAF: "N",
DoctorAG: "N",
DoctorAH: "N",
DoctorAI: "N",
DoctorAJ: "N",
DoctorAK: "N",
DoctorAL: "N",
DoctorAM: "N",
DoctorAN: "Y",
DoctorAO: "Y",
DoctorAP: "Y",
DoctorA: "Y",
DoctorAQ: "Y",
DoctorAR: "Y",
DoctorAS: "Y",
DoctorAT: "N",
DoctorAU: "Y",
DoctorAV: "Y",
DoctorAW: "Y",
DoctorAX: "Y",
DoctorB: "Y",
DoctorAY: "Y",
DoctorAZ: "Y",
DoctorBA: "Y",
DoctorBB: "Y",
DoctorBC: "Y",
DoctorC: "Y",
DoctorBD: "Y",
DoctorBE: "Y",
DoctorBF: "Y"
};
var value_counts = value_count(input),
key;
console.log('counts:');
for (key in value_counts) {
console.log(' property ' + key + ' appeared ' + value_counts[key] + ' times');
}
Upvotes: 0
Reputation: 37876
var myArray = {DoctorD:"N", DoctorE:"N", DoctorF:"N", DoctorG:"N", DoctorH:"N"};
for(var key in myArray){
alert("key:" + key + " has value:" + myArray[key]);
}
working example: http://jsfiddle.net/a23vd/
Upvotes: 0
Reputation: 7466
You have an associative array, to iterate it you can use for in
loop.
var ary = { DoctorD:"N", DoctorE:"N", DoctorF:"N", DoctorG:"N", DoctorH:"N", DoctorI:"N", DoctorJ:"N", DoctorK:"N", DoctorL:"N", DoctorM:"N", DoctorN:"N", DoctorO:"N", DoctorP:"N", DoctorQ:"N", DoctorR:"N", DoctorS:"N", DoctorT:"N", DoctorU:"N", DoctorV:"Y", DoctorW:"N", DoctorX:"N", DoctorY:"N", DoctorZ:"N", DoctorAA:"N", DoctorAB:"N", DoctorAC:"N", DoctorAE:"N", DoctorAF:"N", DoctorAG:"N", DoctorAH:"N", DoctorAI:"N", DoctorAJ:"N", DoctorAK:"N", DoctorAL:"N", DoctorAM:"N", DoctorAN:"Y", DoctorAO:"Y", DoctorAP:"Y", DoctorA:"Y", DoctorAQ:"Y", DoctorAR:"Y", DoctorAS:"Y", DoctorAT:"N", DoctorAU:"Y", DoctorAV:"Y", DoctorAW:"Y", DoctorAX:"Y", DoctorB:"Y", DoctorAY:"Y", DoctorAZ:"Y", DoctorBA:"Y", DoctorBB:"Y", DoctorBC:"Y", DoctorC:"Y", DoctorBD:"Y", DoctorBE:"Y", DoctorBF:"Y"};
for (var key in ary) {
if (ary.hasOwnProperty(key)) {
var value = ary[key];
console.log(value);
}
}
You can learn more about for in
loop: http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/
Upvotes: 1