检查来自类似子对象属性的值

时间:2018-02-19 17:07:45

标签: javascript

我处于类似情况:

 this.state = {    
            globalState:     {
                aState:  { errorState: null, errorReport: ''},
                bState:  { errorState: null, errorReport: ''},
                nazfState:  { errorState: null, errorReport: ''},
                prdState:  { errorState: null, errorReport: ''},
                vdState:  { errorState: null, errorReport: ''},
                tesState:  { errorState: null, errorReport: ''}
            },
            ....

有没有办法检查所有errorState属性是否为空?

现在我以这种方式访问​​所有对象:

 Object.keys(this.state.globalState).forEach(function(key,index) {
            console.log( key); 

        });

打印键(astate,bstate ......)...但无法访问所有属性\对象的errorState值。

3 个答案:

答案 0 :(得分:0)

尝试以这种方式访问​​:

Object.keys(state.globalState).forEach(function(key,index) {
    console.log(state.globalState[key].errorState);
});

答案 1 :(得分:0)

errorState访问state.globalState[key].errorState。对于演示,我已替换this

state.globalState[key]将为aStatebState等提供每个state.globalState[key].errorState var state = { globalState: { aState: { errorState: null, errorReport: '' }, bState: { errorState: null, errorReport: '' }, nazfState: { errorState: null, errorReport: '' }, prdState: { errorState: null, errorReport: '' }, vdState: { errorState: null, errorReport: '' }, tesState: { errorState: null, errorReport: '' } } } Object.keys(state.globalState).forEach(function(key, index) { console.log(state.globalState[key].errorState); });将提供密钥的值。

使用方括号,变量用于从对象

获取值

if (e1.length() == 0)
    Toast.makeText(mainActivity.this, "String is Empty");

答案 2 :(得分:0)

使用every检查所有错误状态是否为null或特定值。附注 - 不确定为什么要使用this,我可能无法获得整个上下文,下面的代码与您正在寻找的内容相近

   this.state = {    
            globalState:     {
                aState:  { errorState: null, errorReport: ''},
                bState:  { errorState: null, errorReport: ''},
                nazfState:  { errorState: null, errorReport: ''},
                prdState:  { errorState: null, errorReport: ''},
                vdState:  { errorState: null, errorReport: ''},
                tesState:  { errorState: null, errorReport: ''}
            }
            }

       let errStates = []          
       Object.keys(this.state.globalState).forEach(k => errStates.push(k => k.errorState));
      
      console.log(errStates.every(e => {return e.errorState == null}));