Javascript 按角度将项目推送到具有多个if事例的数组
我不熟悉打字脚本,正在做一个有角度的项目 我正在进行API调用,并对接收到的数据进行一些操作:Javascript 按角度将项目推送到具有多个if事例的数组,javascript,angular,typescript,Javascript,Angular,Typescript,我不熟悉打字脚本,正在做一个有角度的项目 我正在进行API调用,并对接收到的数据进行一些操作: public data_Config: IConfig[] = []; this.getService.Data(input).subscribe( data => { this.data_Config = data; this.data_Config.forEach(itm => { if(itm.aFl == 'Y')
public data_Config: IConfig[] = [];
this.getService.Data(input).subscribe(
data => {
this.data_Config = data;
this.data_Config.forEach(itm => {
if(itm.aFl == 'Y'){
itm.Levels.push('A')
}
if(itm.bFl == 'Y'){
itm.Levels.push('B')
}
if(itm.cFl == 'Y'){
itm.Levels.push('C')
}
});
this.dataSource_prgmConfiguration = new MatTableDataSource(this.data_prgmConfiguration);
this.dataSource_prgmConfiguration.paginator = this.paginatorPrgmConfiguration;
this.dataSource_prgmConfiguration.sort = this.sortPrgmConfiguration;
});
IConfig
具有多个属性,包括10-12个标志属性,如aFl
、bFl
、cFl
。在曾经为真的标志属性中,我想将它们添加到数组中。我使用if条件以一种简单的方式完成了它,但是如果需要11-12个标志,有没有更好的方法来实现它
添加IConfig
您可以这样做:
Object.keys(this.dataConfig).filter(key => key.indexOf('Fl') != -1).forEach(flag => {
if(this.dataConfig[flag] == 'Y')
itm.Levels.push(this.dataConfig[glag].charAt(0).toUpperCase());
}
只有当您的所有标志与xFl的时间相同时,此代码才有效,其中x是某个字母,并且此标志引用需要推送到Levels数组的同一大写字母。
一点说明:
首先从对象中提取作为标志的键,然后对它们进行迭代并检查您的条件。您可以执行以下操作:
Object.keys(this.dataConfig).filter(key => key.indexOf('Fl') != -1).forEach(flag => {
if(this.dataConfig[flag] == 'Y')
itm.Levels.push(this.dataConfig[glag].charAt(0).toUpperCase());
}
只有当您的所有标志与xFl的时间相同时,此代码才有效,其中x是某个字母,并且此标志引用需要推送到Levels数组的同一大写字母。
一点说明:
首先从对象中提取作为标志的键,然后对它们进行迭代并检查您的条件。根据需求有多种方法 我假设
IConfig
接口只有aF1、bF1、cF1…
布尔属性,
然后你可以像下面这样
const booleanProperties = ['aF1', 'bF1', 'cF1']; // define bool properties in array
this.data_Config.forEach(itm => {
Object.keys(itm).forEah(key=>{
if(booleanProperties.includes(key) && this.data_Config[key] === 'Y'){
itm.Levels.push('whatever'); // I don't know exactly what 'A','B' n all represent }
})
});
解决方案:1
this.data_Config.forEach(itm => {
Object.keys(itm).forEah(key=>{
if(this.data_Config[key] === 'Y'){
itm.Levels.push('whatever'); // I don't know exactly what 'A','B' n all represent }
})
});
解决方案:2
假设IConfig
接口具有一些其他属性,例如height
,width
一些非布尔属性,在这种情况下,您可以执行以下操作:
const booleanProperties = ['aF1', 'bF1', 'cF1']; // define bool properties in array
this.data_Config.forEach(itm => {
Object.keys(itm).forEah(key=>{
if(booleanProperties.includes(key) && this.data_Config[key] === 'Y'){
itm.Levels.push('whatever'); // I don't know exactly what 'A','B' n all represent }
})
});
根据需求有多种方式 我假设
IConfig
接口只有aF1、bF1、cF1…
布尔属性,
然后你可以像下面这样
const booleanProperties = ['aF1', 'bF1', 'cF1']; // define bool properties in array
this.data_Config.forEach(itm => {
Object.keys(itm).forEah(key=>{
if(booleanProperties.includes(key) && this.data_Config[key] === 'Y'){
itm.Levels.push('whatever'); // I don't know exactly what 'A','B' n all represent }
})
});
解决方案:1
this.data_Config.forEach(itm => {
Object.keys(itm).forEah(key=>{
if(this.data_Config[key] === 'Y'){
itm.Levels.push('whatever'); // I don't know exactly what 'A','B' n all represent }
})
});
解决方案:2
假设IConfig
接口具有一些其他属性,例如height
,width
一些非布尔属性,在这种情况下,您可以执行以下操作:
const booleanProperties = ['aF1', 'bF1', 'cF1']; // define bool properties in array
this.data_Config.forEach(itm => {
Object.keys(itm).forEah(key=>{
if(booleanProperties.includes(key) && this.data_Config[key] === 'Y'){
itm.Levels.push('whatever'); // I don't know exactly what 'A','B' n all represent }
})
});
在typescript中,您可以像这样创建一个
ENUM
enum enumVal {
aFl = 'A',
bFl = 'B'
}
然后在迭代中检查值并将其添加到所需的结果中
result:any[] = [];
this.data_Config.forEach(key=>{
if(this.data_Config[key] === 'Y'){
result.push(enumVal[key]);
}
});
在typescript中,您可以像这样创建一个
ENUM
enum enumVal {
aFl = 'A',
bFl = 'B'
}
然后在迭代中检查值并将其添加到所需的结果中
result:any[] = [];
this.data_Config.forEach(key=>{
if(this.data_Config[key] === 'Y'){
result.push(enumVal[key]);
}
});
一种更简单的方法是将需要存储在对象{aF1:'A',bF1:'b',…}中的所有标志及其相应的数据存储起来。然后遍历上述对象键,如果项具有键,则插入标记对象的相应值。请共享
IConfig
definitionaddedIConfig
definition一种更简单的方法是存储所有标记及其需要存储在对象中的对应数据{aF1:'a',bF1:'b',…}. 然后遍历上述对象键,然后如果项具有键,则插入标记对象的相应值。请共享IConfig
definitionaddedIConfig
definitionI获取错误vendor.js:77227错误类型错误:无法读取未定义的属性“push”
itm.Levels
是吗阵列?它应该是数组。我得到一个错误vendor.js:77227错误类型错误:无法读取未定义的属性“push”
itm.Levels
它是数组吗?它应该是数组。我收到一个错误vendor.js:77227错误类型错误:无法读取未定义的属性“push”
。我收到一个错误vendor.js:77227错误类型错误:无法读取未定义的属性“push”
。