如何从数组中过滤数据并应用条件角度 8
how to filter data from array and apply conditions angular8
我正在从 api 动态获取以下示例数据。
任何人都可以建议如何根据数据应用条件。
您需要检查数组中的值
if (this.tagSchemaDetails.schema_context.providers.indexOf('All') < 0) {
request['schema_context'] = this.createConditionsData();
}
由于您的提供程序是字符串数组,您只是将其与字符串进行比较,因此在所有情况下它都会验证条件。
所以你必须检查数组中的字符串并将它们与 'All' 进行比较以正确检查条件。
所以你的代码应该是这样的:
if(!this.tabSchemaDetails.schema_context.providers.includes('All')){
request['schema_context']=this.createConditionsData();
}
正如其他人在这里所说的,您必须检查 "providers" 数组中的值。因为您只需要检查 provider != 'All' 您可以简单地检查数组中的第一个元素,如下所示。
if (this.tagSchemaDetails.schema_context.providers[0] !== 'All') {
request['schema_context'] = this.createConditionsData();
}
我正在从 api 动态获取以下示例数据。
任何人都可以建议如何根据数据应用条件。
您需要检查数组中的值
if (this.tagSchemaDetails.schema_context.providers.indexOf('All') < 0) {
request['schema_context'] = this.createConditionsData();
}
由于您的提供程序是字符串数组,您只是将其与字符串进行比较,因此在所有情况下它都会验证条件。
所以你必须检查数组中的字符串并将它们与 'All' 进行比较以正确检查条件。 所以你的代码应该是这样的:
if(!this.tabSchemaDetails.schema_context.providers.includes('All')){
request['schema_context']=this.createConditionsData();
}
正如其他人在这里所说的,您必须检查 "providers" 数组中的值。因为您只需要检查 provider != 'All' 您可以简单地检查数组中的第一个元素,如下所示。
if (this.tagSchemaDetails.schema_context.providers[0] !== 'All') {
request['schema_context'] = this.createConditionsData();
}