这是我的目标,我如何获得所有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.Any帮助将不胜感激。这可能吗?或者其他任何解决方案吗?
答案 0 :(得分:1)
你有一个关联数组,要迭代它你可以使用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/
答案 1 :(得分: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]);
}
答案 2 :(得分:0)
为了你的目的可能有点矫枉过正,但你要求计数。这将返回一个对象,其中包含原始对象中找到的每个值的计数。您可能想要进行更多类型检查。例如,确保对象的值是有意义的或具有预期值。使用下面的函数计算{'DoctorA': ['N','S'], 'DoctorB': ['N','S']};
有点无意义。
以下是一个工作示例:http://jsfiddle.net/Fjfm8/
有关For .. In方法的更多内容: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');
}