如何计算一个对象的值

How to Count the Values of an Object

本文关键字:一个对象 计算 何计算      更新时间:2023-09-26

这是我的对象,我怎么能得到所有的N计数和所有的S计数。至少我可以从这个数组中访问N和Y变量。

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"

在javascript。任何帮助都会很感激。这可能吗?或者还有其他解吗?

你有一个关联数组,你可以使用for in循环来迭代它。

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);
    }
}

您可以了解更多关于for in循环:http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/

var myArray = {DoctorD:"N", DoctorE:"N", DoctorF:"N", DoctorG:"N", DoctorH:"N"};
for(var key in myArray){
    alert("key:" + key  + " has value:" + myArray[key]);
}

工作示例: http://jsfiddle.net/a23vd/

对于您的目的来说可能有点夸张,但是您要求计数。这将返回一个对象,其中包含在原始对象中找到的每个值的计数。您可能需要进行更多的类型检查。例如,确保对象的值是有意义的值或期望值。使用下面的函数来计算{'DoctorA': ['N','S'], 'DoctorB': ['N','S']};有点无意义。

  • 下面是一个工作示例:http://jsfiddle.net/Fjfm8/

  • 更多关于For…方法: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');
}