1

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?

matchew
  • 19,195
  • 5
  • 44
  • 48
user3580511
  • 19
  • 1
  • 6
  • first of all it's an Object notation – ashishmaurya May 02 '14 at 06:09
  • I modified your title slightly to reflect your question. If the question is really about the `for .. in` method of looping through object keys this maybe a dupe -- http://stackoverflow.com/questions/8312459/iterate-through-object-properties – matchew May 02 '14 at 06:44

3 Answers3

1

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/

Amy
  • 7,388
  • 2
  • 20
  • 31
0
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/

doniyor
  • 36,596
  • 57
  • 175
  • 260
0

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.


/**
 * 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');
}
matchew
  • 19,195
  • 5
  • 44
  • 48