代码之家  ›  专栏  ›  技术社区  ›  alim1990

角度7和对象搜索如何找到严格的值

  •  0
  • alim1990  · 技术社区  · 6 年前

    我正在搜索值 yes 在野外 hhead 从服务器返回的对象:

    Object.keys(this.data).forEach(key => {
        if (this.data[key].hhead === 'yes') {
        console.log('Yes '+(this.data[key].hhead === 'yes'))
        this.snackBar.open('This household already have ' + this.data[key].far + ' ' + this.data[key].lar + ' (id: ' + this.data[key].iid + ' ) as a head of household', 'Close', {
            panelClass: 'error'
        });
        }
        else {
        console.log('No '+(this.data[key].hhead === 'no'))
        if (data['age'] <= 17 && data['age'] < this.maxAge && (selectedFr == "Head Of Household")) {
    
            let message = 'This individual is not the oldest in his family to be the head of household. Do you want to complete this action ?';
            this.openDialog(message, updateType, ind_id, newSts, newMs, newFr, newHH, oldHH, missingData);
        }
        }
    });
    

    这个脚本的问题是 if else 是真的。所以这两个脚本都会运行。

    原因是,在第一个条件下,一旦它发现 值,条件变为真。

    no 它会跑的。

    数组如下:

    enter image description here

    所以我需要的是 在所有行中,运行 其他的 部分。如果找到了 至少 运行第一个条件。

    1 回复  |  直到 6 年前
        1
  •  1
  •   piotr szybicki    6 年前

    我认为你试图从错误的角度来解决这个问题。必须先扫描集合,然后运行代码:

    var mached = this.data.every(t => t.hhead == 'yes'); //this will print true
    
    Object.keys(this.data).forEach(key => {
        if (mached) {
            console.log('Yes '+(this.data[key].hhead === 'yes'))
            this.snackBar.open('This household already have ' + this.data[key].far + ' ' + this.data[key].lar + ' (id: ' + this.data[key].iid + ' ) as a head of household', 'Close', {
               panelClass: 'error'
            });
        } else {
            console.log('No '+(this.data[key].hhead === 'no'))
            if (data['age'] <= 17 && data['age'] < this.maxAge && (selectedFr == "Head Of Household")) {
    
            let message = 'This individual is not the oldest in his family to be the head of household. Do you want to complete this action ?';
            this.openDialog(message, updateType, ind_id, newSts, newMs, newFr, newHH, oldHH, missingData);
        }
        }
    });
    
    推荐文章