[英]Loop through an object of objects
有一个包含以下形式对象的对象:
bigObject: {
"a - values": { atr: true}
"a - items": { atr: true}
"a - others": { atr: false}
"b - values": { atr: true}
"b - items": { atr: true}
"b - others": { atr: false}
"c - values": { atr: false}
"c - items": { atr: true}
"c - others": { atr: false}
}
我在函数内部使用此对象来检查每次属性之一更改其布尔值的情况: onButtonClicked(item)
它的功能类似于:
onButtonClicked(item) {
bigObject[item.id].atr= !bigObject[item.id].atr;
}
在此函数内部,我想将它们拆分,以便能够分别检查以a
, b
和c
开头的对象的值。 为此,我做到了: const toCheck = item.id.split("-")[0];
这可以正常工作,如果单击该对象,它将仅使用以a开头的对象。
下一步是检查特定字母是否同时具有true
和false
属性。
为此,我尝试这样做:
let countFalse = 0;
let countTrue = 0;
bigObject.forEach(x => {
if ((x.split("-")[0]) === toCheck) {
if (x.atr) {
countTrue++;
} else countFalse++;
}
if (countTrue && countFalse) {
console.log("has both true and false attributes");
} else console.log("nope");
});
因此,我拆分了原始名称以消除(值,项目,其他)值,然后尝试计算true和false属性。 如果两者都存在,请显示一条消息,说明否。
出了点问题,但我不明白。 有任何想法吗?
你可以遍历与分裂的条目' - '
而不是'-'
。
var bigObject = { "a - values": { atr: true }, "a - items": { atr: true }, "a - others": { atr: false }, "b - values": { atr: true }, "b - items": { atr: true }, "b - others": { atr: false }, "c - values": { atr: false }, "c - items": { atr: true }, "c - others": { atr: false } }, countFalse = 0, countTrue = 0, toCheck = 'a'; Object.entries(bigObject).forEach(([k, v]) => { if (k.split(" - ")[0] !== toCheck) { return; } if (v.atr) { countTrue++; } else { countFalse++; } }); if (countTrue && countFalse) { console.log("has both true and false attributes"); } else { console.log("nope"); }
带有对象计数的更紧凑版本。
var object = { "a - values": { atr: true }, "a - items": { atr: true }, "a - others": { atr: false }, "b - values": { atr: true }, "b - items": { atr: true }, "b - others": { atr: false }, "c - values": { atr: false }, "c - items": { atr: true }, "c - others": { atr: false } }, count = { false: 0, true: 0 }, toCheck = 'a'; Object.entries(object).forEach(([k, { atr }]) => count[atr] += k.startsWith(toCheck)); if (count.true && count.false) { console.log("has both true and false attributes"); } else { console.log("nope"); } console.log(count);
据我所知,forEach不会遍历对象。 我建议你用
const bigObjectKeys = Object.keys(bigObject)
比这样迭代:
bigObjectKeys.forEach(element => {bigObject [element] ...})
或使用lodash forEach,它可以遍历对象。
发生错误并要解决的问题:
keys
/ entries
。 -
分割,而不用-
分割。 if
,您已通过所有元素迭代之后。 var countFalse = 0; var countTrue = 0; var bigObject= { "a - values": { atr: true}, "a - items": { atr: true}, "a - others": { atr: false}, "b - values": { atr: true}, "b - items": { atr: true}, "b - others": { atr: false}, "c - values": { atr: false}, "c - items": { atr: true}, "c - others": { atr: false} } var toCheck = "a"; Object.keys(bigObject).forEach(x => { if ((x.split(" - ")[0]) === toCheck) { if (bigObject[x].atr) { countTrue++; } else countFalse++; } }); if (countTrue && countFalse) { console.log("has both true and false attributes"); } else console.log("nope");
为了提高效率,
var countFalse = 0, countTrue = 0; var bigObject= { "a - values": { atr: true}, "a - items": { atr: false}, "a - others": { atr: false}, "b - values": { atr: true}, "b - items": { atr: true}, "b - others": { atr: false}, "c - values": { atr: false}, "c - items": { atr: true}, "c - others": { atr: false} } var toCheck = "a"; Object.keys(bigObject).forEach(x => { if ((x.split(" - ")[0] === toCheck) && !(countTrue>0 && countFalse>0)) { bigObject[x].atr ? countTrue++ : countFalse++; } }); if (countTrue && countFalse) { console.log("has both true and false attributes"); } else console.log("nope");
可以在对象条目上使用Array#filter
和Array#every
const isMatching = (str) =>{ const arr = Object.entries(bigObject).filter(e=> e[0].startsWith(str)); // makes sure every entry has same `atr` as the first entry return arr.every(e => e[1].atr === arr[0][1].atr); } ['a','b','c'].forEach(s => console.log(s, isMatching(s)))
<script> const bigObject= { "a - values": { atr: true}, "a - items": { atr: true}, "a - others": { atr: false}, "b - values": { atr: true}, "b - items": { atr: true}, "b - others": { atr: false}, "c - values": { atr: false}, "c - items": { atr: false}, "c - others": { atr: false} } </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.