我有这个数组:
[
{praticien: "place_1509136116761", H0709: false, H0911: false, H1113: false, H1315: false},
{praticien: "place_1509136116773", H0709: false, H0911: false, H1113: false, H1315: false},
{praticien: "place_1509136116699", H0709: true, H0911: false, H1113: false, H1315: false},
{praticien: "place_1509136116734", H0709: false, H0911: true, H1113: true, H1315: false}
]
我需要一种方法来计算H0709 - H0911 - H1113 - H1315包含值“TRUE”的时间。
任何帮助?
答案 0 :(得分:1)
var arr = [{praticien: "place_1509136116761", H0709: false, H0911: false, H1113: false, H1315: false}, {praticien: "place_1509136116773", H0709: false, H0911: false, H1113: false, H1315: false}, {praticien: "place_1509136116699", H0709: true, H0911: false, H1113: false, H1315: false}, {praticien: "place_1509136116734", H0709: false, H0911: true, H1113: true, H1315: false}]
var result = {
true: 0,
false: 0
}
arr.forEach(function(obj){ // iterates objects in array
for (prop in obj ){ // iterates properties in object (only those of object not inherited)
if (obj.hasOwnProperty(prop)) {
if (obj[prop]) result.true++;
else result.false++;
}
}
});
console.log(result);
这应该计算所有对象中有多少真实和谬误。只有它没有检查属性是否被命名为Hsomething。如果您需要,可以在if (obj.hasOwnProperty(prop))
部分添加其他条件。
以下是演示:fiddle
编辑:只有以' H'开头的属性名称被考虑在内。
上述代码中已更改的行:
if (obj.hasOwnProperty(prop) && prop[0] === 'H') {
可在以下位置找到新演示: new fiddle
注意:console.log
已添加到列表中考虑的属性。对于制作,请将其评论或删除。
答案 1 :(得分:1)
您可以尝试过滤以获取具有您期望的值的对象
function predicate(object) {
return object[H0709];
}
var objectsWithH0709AsTrue = myArray.filter(predicate);
var howManyTimesH0709IsTrue = objectsWithH0709AsTrue.length;
然而,有很多方法可以做到这一点。看看lodash'一些。
答案 2 :(得分:1)
您可以使用array#reduce
循环浏览数据数组,并使用array#filter
查找param
中具有真正价值的所有密钥。
const data = [{praticien: "place_1509136116761", H0709: false, H0911: false, H1113: false, H1315: false},{praticien: "place_1509136116773", H0709: false, H0911: false, H1113: false, H1315: false},{praticien: "place_1509136116699", H0709: true, H0911: false, H1113: false, H1315: false},{praticien: "place_1509136116734", H0709: false, H0911: true, H1113: true, H1315: false}],
keys =['H0709', 'H0911', 'H1113', 'H1315'];
const result = data.reduce((sum,o) => {
sum += keys.filter(k => o[k]).length;
return sum;
},0)
console.log(result);

要获取true
值的所有键的计数,您可以使用array#reduce
和array#forEach
。在array#foreach
内,检查密钥是否具有真值并添加该密钥并计数。
const data = [{praticien: "place_1509136116761", H0709: false, H0911: false, H1113: false, H1315: false},{praticien: "place_1509136116773", H0709: false, H0911: false, H1113: false, H1315: false},{praticien: "place_1509136116699", H0709: true, H0911: false, H1113: false, H1315: false},{praticien: "place_1509136116734", H0709: false, H0911: true, H1113: true, H1315: false}];
const result = data.reduce((res,o) => {
Object
.keys(o)
.forEach(k => {
if(o[k] === true)
res[k] = (res[k] || 0) + 1 ;
});
return res;
},{});
console.log(result);

答案 3 :(得分:0)
var data = [
{
praticien: "place_1509136116761",
H0709: false,
H0911: false,
H1113: false,
H1315: false
},
{
praticien: "place_1509136116773",
H0709: false,
H0911: false,
H1113: false,
H1315: false
},
{
praticien: "place_1509136116699",
H0709: true,
H0911: false,
H1113: false,
H1315: false
},
{
praticien: "place_1509136116734",
H0709: false,
H0911: true,
H1113: true,
H1315: false
}
]
var result = { H0709: 0,H0911: 0,H1113: 0,H1315: 0};
angular.forEach(data, function(obj){
if(obj.H0709){
result[H0709] = result[H0709]++;
}
if(obj.H0911){
result[H0911] = result[H0709]++;
}
if(obj.H1113){
result[H1113] = result[H0709]++;
}
if(obj.H1315){
result[H1315] = result[H0709]++;
}
});
结果为{H0709:1,H0911:1,H1113:1,H1315:0}
表示H0709,H0911和H1113只有一次 TRUE ,而H1315是0次。