gpt4 book ai didi

javascript - 如何计算对象的值

转载 作者:行者123 更新时间:2023-11-28 01:19:19 26 4
gpt4 key购买 nike

这是我的对象,我怎样才能获得所有 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/

关于javascript - 如何计算对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23421800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com