Javascript 查找值为true的属性并返回其消息

Javascript 查找值为true的属性并返回其消息,javascript,object,Javascript,Object,假设我有一个目标 let testObj = { keys : { chkkeys : false, msg : 'test one' }, grtr : { chkgrtr : true, msg : 'test two' }, empt : { chkEmpt : false,

假设我有一个目标

let testObj = {
    keys : {
                chkkeys : false, 
                msg : 'test one'
    },
    grtr    : {
                chkgrtr : true,
                msg : 'test two'
    },
    empt    : {
                chkEmpt : false,
                msg : 'test three'
    }
}
如何找到值为true的属性并返回其消息

例如,这是唯一的真值
chkgrtr:true
,因此该值应返回为
“测试二”

这就是我迄今为止所尝试的

Object.keys(testObj).forEach((item) => {
    Object.keys(testObj[item]).forEach((sI) => {
        if (testObj[item][sI] === true) {
            return testObj[item]['sMsg'];
        }
    })
})

获取
testObj
对象.values
。查找
具有属性的对象,而不是
msg
,该属性的值为
true
,并返回其
msg

让testObj={
关键点:{
奇凯斯:错,
msg:'测试一个'
},
grtr:{
是的,
味精:“测试二”
},
清空:{
切肯普特:错,
味精:“测试三”
}
};
const foundObj=Object.values(testObj)
.find(obj=>Object.entries(obj)
.some(([key,val])=>key!=='msg'&&val)
);
if(foundObj){
console.log(foundObj.msg);

}
只需使用
Array.prototype.find
而不是
forEach
。那会帮你解决的

Object.keys(testObj).find((item) => {
    return Object.keys(testObj[item]).find((sI) => {
        if (testObj[item][sI] === true) {
            return testObj[item]['sMsg'];
        }
    })
})
**Object.entries()**在这种情况下可能会对您有所帮助。
设testObj={
关键点:{
奇凯斯:错,
msg:'测试一个'
},
grtr:{
是的,
味精:“测试二”
},
清空:{
切肯普特:错,
味精:“测试三”
}
}
让Entries=Object.Entries(testObj);
对于(var i=0;i功能方式:

Object.values(testObj).find(value=> value[Object.keys(value).find(key=>key != 'msg')]).msg
Object.values(testObj).find(value=> value[Object.keys(value).find(key=>key != 'msg')]).msg