简体   繁体   English

从 Typescript 中的动态字符串数组中获取值

[英]Get values from dynamic string array in Typescript

In angular material form, I have a field with mat-option which contains list of options(ie, a, b, c, d, e).在 angular 材料表单中,我有一个带有 mat-option 的字段,其中包含选项列表(即 a、b、c、d、e)。 When user selects/unselects one particular option(ie, c), I need to do some manipulation.当用户选择/取消选择一个特定选项(即 c)时,我需要进行一些操作。 But I am getting undefined error, even if I select an option 'c' without selecting any other option.但是我得到了未定义的错误,即使我 select 一个选项 'c' 没有选择任何其他选项。

I can understand that it is due to the index value[2] that I have used.我可以理解这是由于我使用的索引值[2]。 I am not sure on how to identify if the user selects option 'C' or not Since it's index value may increase or decrease dynamically based on user selection.我不确定如何识别用户是否选择选项“C”,因为它的索引值可能会根据用户选择动态增加或减少。

Response array may look like:响应数组可能如下所示:

value = {'d','c'} or value = {'c','b','a'} or value = {}

if(value[2] === 'c') {
   console.log('c');
 } 

Can someone help me to write typescript code?有人可以帮我写 typescript 代码吗?

Maybe there's a missing thing in your code.也许您的代码中缺少一些东西。 Since you told us that you use mat-option , I tried to create sample code using that.由于您告诉我们您使用mat-option ,因此我尝试使用它创建示例代码。

component.html组件.html

<h4>mat select</h4>
<mat-form-field>
  <mat-label>Choose option</mat-label>
  <mat-select [formControl]="optionControl" required (valueChange)="onChange($event)">
    <mat-option>--</mat-option>
    <mat-option *ngFor="let option of options" [value]="option">
      {{option}}
    </mat-option>
  </mat-select>  
</mat-form-field>

<div>
  <p>Selected value</p>
  {{ optionControl.value }}
</div>

component.ts组件.ts

import {Component} from '@angular/core';
import {FormControl, Validators} from '@angular/forms';

@Component({
  selector: 'app-example',
  templateUrl: 'app-example.html',
  styleUrls: ['app-example.css'],
})
export class AppExampleComponent {
  optionControl = new FormControl('', Validators.required);  
  options = ['a', 'b', 'c']; // this is static but should be dynamic for your case

  onChange(value: string) { // value will be selected option
    if (value === 'c') {
      console.log('c');
    }
  }
}

Take a look at the action on StackBlitz看看StackBlitz上的动作

I don't think value = {'d','c'} or value = {'c','b','a'} or value = {} is something legit JavaScript/TypeScript syntax for objects.我不认为value = {'d','c'} or value = {'c','b','a'} or value = {}是对象的合法 JavaScript/TypeScript 语法。 Unless you were referring to something like {c: 'c'}除非你指的是像{c: 'c'}

In that case it's an object and you need either Object.keys() or Object.values() to convert it to an array first.在这种情况下,它是 object 并且您需要Object.keys()Object.values()首先将其转换为数组。

 const value = { c: 'c', b: 'b', a: 'a' }; if(Object.values(value).includes('c')) { console.log('c'); } else { console.log('NO c'); }

Let's say user can select multiple options.假设用户可以 select 多个选项。 You need to subscribe to selection change, or call a method from HTML whenever change happens.您需要订阅选择更改,或在发生更改时从HTML调用方法。 For example all selected values are stored in values variable.例如,所有选定的值都存储在values变量中。 How are selected values stored in values depends on the way you detect change.所选值如何存储在values中取决于您检测更改的方式。 After every selection change split that variable to an array like this:每次选择更改后,将该变量拆分为如下数组:

let valuesArray = values.toString().split(',');

Then check every array element:然后检查每个数组元素:

valuesArray.forEach(v => { if (v === 'c') { console.log('c'); } });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM