如何检查对象的typeSoftware是否具有(-1)值并将其从中删除?

时间:2019-04-17 11:06:10

标签: javascript angular string

我有这个对象,类型软件是他的财产,在某些情况下我为-1。我想先将其从对象中删除,然后再去服务器

  

对象companyName:“ dsfdsfdsfsd” companyVatId:“ dsfdsfdsf”   公司网站:“ dfdsfsdf”国家/地区:“ DZ”电子邮件:“ sdfsdfs@gmail.com”   firstName:“ sdfsdf” lastName:“ fsdfs”其他:“ dsfsdf”电话:   “ sdfsdfsdfsf”在公司中的位置:“ dfsdfsd”产品说明:   “ dsfsdfsdf”产品名称:“ sdfdsfdsf”类型软件:“ 2,-1”

结果应该是这种情况:

  

对象companyName:“ dsfdsfdsfsd” companyVatId:“ dsfdsfdsf”   公司网站:“ dfdsfsdf”国家/地区:“ DZ”电子邮件:“ sdfsdfs@gmail.com”   firstName:“ sdfsdf” lastName:“ fsdfs”其他:“ dsfsdf”电话:   “ sdfsdfsdfsf”在公司中的位置:“ dfsdfsd”产品说明:   “ dsfsdfsdf”产品名称:“ sdfdsfdsf”类型软件:“ 2”

这是我的代码:

  onRegisterPartnerSubmitted() {
const typeSoftware = this.secondRegisterPartnerForm.controls['typeSoftware'].value.join(', ');
const cloneSecondRegisterPartnerForm = Object.assign(this.secondRegisterPartnerForm.value, { typeSoftware });


// Remove here the -1 "others" from the payload object this.partnerData,

if (this.firstRegisterPartnerForm.valid && this.secondRegisterPartnerForm.valid && this.isPrivacyChecked) {
  this.registerPartnerSubmitted.emit(this.partnerData);
} else {
  this.errorMessage = `Please check the privacy agreement to submit the form`;
}

}

更改(事件){

console.log(event);

if (this.secondRegisterPartnerForm.controls.typeSoftware.value.length > 3) {

  this.secondRegisterPartnerForm.controls.typeSoftware.setValue(this.lastSelectedTypesOfSoftware);

}

this.lastSelectedTypesOfSoftware = this.secondRegisterPartnerForm.controls.typeSoftware.value;
if (this.lastSelectedTypesOfSoftware.find(x => x === -1)) {
  this.show = true;
} else {
  this.show = false;
}

}

我在前端使用:

            <mat-form-field>
          <mat-select
            placeholder="Type of Software* (You can choose up to three categories)"
            formControlName="typeSoftware" multiple (selectionChange)="changed($event)" > 
            <mat-option
              *ngFor="let category of categories$ | async"
              id="typeSoftware"
              [value]="category.id">
              {{category.name}}
            </mat-option>
            <mat-option 
            [value]="-1">
              Other
            </mat-option>
          </mat-select>
        </mat-form-field>

3 个答案:

答案 0 :(得分:1)

您可以使用正则表达式或数组函数来实现。 如果您的对象是字符串,则必须先调用JSON.parse(o)

//o is your Object
o = {typeSoftware:  '2 , -1 ,6'}
o.typeSoftware = o.typeSoftware.split(',').map(x => x.trim()).filter(y => y !== '-1').join(', ');
console.log(o.typeSoftware)

答案 1 :(得分:1)

您可以使用替换捕获组

\btypeSoftware:\s*"([^"]+)"

通过这种模式,我们捕获了typeSoftware: " followed by value "

在replace函数的回调中,我们获取捕获的组并将其拆分到,上,然后我们过滤出所需的值并将其加入,

let str = `Object companyName: "dsfdsfdsfsd" companyVatId: "dsfdsfdsf" companyWebsite: "dfdsfsdf" country: "DZ" email: "sdfsdfs@gmail.com" firstName: "sdfsdf" lastName: "fsdfs" other: "dsfsdf" phone: "sdfsdfsdfsf" positionInTheCompany: "dfsdfsd" productDescription: "dsfsdfsdf" productName: "sdfdsfdsf" typeSoftware: "2, -1" `

let final = (str) =>str.replace(/\btypeSoftware:\s*"([^"]+)"/g, (match,g1)=>{
  return `typeSoftware:"${g1.split(',').filter(e=> e.trim() !== '-1' && e).join(',')}"`
})

console.log(final(str))

// this will handle following case too

console.log(final(`typeSoftware:"-1, 2, 5, 9"`))
console.log(final(`typeSoftware:"2, -1, 4,"`))

答案 2 :(得分:0)

此正则表达式将帮助您删除字符串中的最后一个-1, -1

'2, -1'.replace(/(,|^)\s*-1$/, '')

这里有个例子:

const typeSoftware = '-1';
const result = typeSoftware.replace(/(,|^)\s?-1$/, '');

console.log(typeSoftware);
console.log(result);