【问题标题】:Angular 7 and Objects search how to find strict valuesAngular 7 和 Objects 搜索如何找到严格的值
【发布时间】:2018-12-07 13:36:38
【问题描述】:

我正在从服务器返回的对象的字段hhead 中搜索值yes

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);
    }
    }
});

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

原因是,在第一个条件下,一旦找到yes 值,条件就变为真。

第二个,一旦找到no,它就会运行。

数组是这样的:

所以我需要的是如果一个数组在所有行中只包含no,则运行else 部分。如果它发现至少 yes 运行第一个条件。

【问题讨论】:

标签: arrays angular object angular7


【解决方案1】:

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

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);
    }
    }
});

【讨论】:

  • 我会在星期一检查并回复您。
猜你喜欢
  • 1970-01-01
  • 2018-12-06
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多