Javascript 如何计算对象的值

Javascript 如何计算对象的值,javascript,Javascript,这是我的目标,我怎样才能得到所有的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"

这是我的目标,我怎样才能得到所有的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"
如果有任何帮助,我们将不胜感激。这可能吗?或者有其他的解决办法吗

var myArray = {DoctorD:"N", DoctorE:"N", DoctorF:"N", DoctorG:"N", DoctorH:"N"};

for(var key in myArray){
    alert("key:" + key  + " has value:" + myArray[key]);
}

工作示例:

您有一个关联数组,要迭代它,您可以使用
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);
    }
}

您可以在循环中了解有关
的更多信息:

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

  • 以下是一个工作示例:

  • 更多关于。。在方法上:


/**
*对一组键值对中的值进行计数,并对每个值的出现次数进行计数
*@param{object}input-包含键值对的对象
*@throws{error}TypeError-如果输入不是对象
*@return{object}counts-为每个值的出现次数设置键的对象
*/
var值\ U计数=函数(输入){
if(Object.prototype.toString.call(输入)='[Object Object]''){
var计数,键,val;
控制台日志(输入);
计数={};
用于(输入键){
val=输入[键];
if(input.hasOwnProperty(key)){
如果(计算hasOwnProperty(val)){
计数[val]+=1;
}否则{
计数[val]=1;
}
}
}
返回计数;
}否则{
抛出新的TypeError(“预期输入为对象”);
}
};
//输入
变量输入={
博士:“N”,
医生:“N”,
博士:“N”,
博士组织:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
医生:“N”,
博士:“N”,
博士:“N”,
博士:“Y”,
博士:“N”,
博士:“N”,
教义:“N”,
博士:“N”,
博士:“N”,
博士B:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“N”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
医生:“Y”,
博士:“Y”,
博士学位:“N”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
博士B:“Y”,
博士C:“Y”,
博士:“Y”,
博士:“Y”,
博士:“Y”,
博士:“Y”
};
var值计数=值计数(输入),
钥匙
log('counts:');
用于(输入值\单位计数){
log('property'+key++'出现'+value_counts[key]+'times');
}

首先,它是一个对象符号。我稍微修改了你的标题,以反映你的问题。如果问题真的是关于..的
。。在
循环对象键的方法中,这可能是一个重复——感谢您最早的响应
/**
 * 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');
}