gpt4 book ai didi

javascript - 在 JavaScript 中按对象参数过滤和计数对象的 JSON 对象

转载 作者:行者123 更新时间:2023-11-28 14:44:18 24 4
gpt4 key购买 nike

我有一个与 JSON 对象 数据的 filtercount 相关的问题。

如何过滤计数其中SIT = false对象 ?在示例数据中,该值为 3。

SIT 总数是多少?在示例数据中,该值为 4。

我想对 PROD 做同样的事情。

我尝试过以下方法:

var SIT = response.filter(function (obj) {
return obj.environment === "SIT";
});

var SIT = [];
SIT = response.data.cis.filter(function (obj) {
return obj.environment === "SIT";
});

这是我的数据:

{
"cis": {
"dsbgchop193": {
"environment": "SIT",
"is_compliant": false
},
"dsbgchop194": {
"environment": "SIT",
"is_compliant": true
},
"dsbgchop195": {
"environment": "SIT",
"is_compliant": false
},
"dsbgchop196": {
"environment": "SIT",
"is_compliant": false
},
"id": "2017.10.17",
"psbgwais1v": {
"environment": "PROD",
"is_compliant": false
},
"psbgwais2v": {
"environment": "PROD",
"is_compliant": false
},
"rsbgwais1v": {
"environment": "PROD",
"is_compliant": false
},
"rsbgwais2v": {
"environment": "PROD",
"is_compliant": true
}
},
"rating": "",
"ssl": {}
}

最佳答案

var data = {
"cis": {
"dsbgchop193": {
"environment": "SIT",
"is_compliant": false
},
"dsbgchop194": {
"environment": "SIT",
"is_compliant": true
},
"dsbgchop195": {
"environment": "SIT",
"is_compliant": false
},
"dsbgchop196": {
"environment": "SIT",
"is_compliant": false
},
"id": "2017.10.17",
"psbgwais1v": {
"environment": "PROD",
"is_compliant": false
},
"psbgwais2v": {
"environment": "PROD",
"is_compliant": false
},
"rsbgwais1v": {
"environment": "PROD",
"is_compliant": false
},
"rsbgwais2v": {
"environment": "PROD",
"is_compliant": true
}
},
"rating": "",
"ssl": {}
}

var statistics = Object.keys(data.cis).reduce(function (stats, key) {
var item = data.cis[key];
if (item.environment === 'SIT') {
stats.totalCount += 1;

if (item.is_compliant) {
stats.compliantCount += 1;
}
}
return stats;
}, { totalCount: 0, compliantCount: 0 });

console.log('statistics : ', statistics);
.as-console-wrapper { max-height: 100%!important; top: 0; }

关于javascript - 在 JavaScript 中按对象参数过滤和计数对象的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47001528/

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