如何使用Angular 6从地图返回client_id

时间:2018-10-10 08:51:26

标签: angular typescript

我有这个json客户端:

    {
        "StatusCode": 0,
        "StatusMessage": "OK",
        "StatusDescription": [
            {
                "client_id": "123",
                "client_name": "0prVDhbp",
              },
            {
                "client_id": "5876856",
                "client_name": "GKlG1hbp",
            },
              ....
            {
                "client_id": "6896868",
                "client_name": "name",
            }
     ]
  }

我使用Map获取输入值:

  _data = new Map<string, any>();

  getData(key) {
    console.log(this._data) // this return only client_name
    return this._data.get(key);
  }

如何返回client_id而不是client_name?

在组件中,我像这样使用getData()

  let client_id = '';
    if (this.ss.getData('client_id')) {
      client_id = this.ss.getData('client_id');

    }

我使所有客户端都具有此功能:

this.cs.getAllClients().subscribe(
  client => {
    this.client = client.map((clients) => {
      this.filteredOptionsClient = this.client_id.valueChanges.pipe(
        startWith(''),
        map(val => this.filterClient(val))
      );
      return new Client(clients);
    });
    if (this.ss.getData('client_id')) {
      const client_id = this.ss.getData('client_id');
      this.addsale.controls.client_id.setValue(client_id)
      let selectedClient = new Client('')
      this.selectedClient = null;
      for (let i = 0; i < this.client.length; i++) {
        if (this.client[i].clientName === client_id) {
          this.selectedClient = this.client[i];
        }
      }
    }
  }
);

在这一部分if (this.client[i].clientName === client_id) {中,我的clientName与client_id相比较,并且为true。当我有很多同名的客户时,客户会感到沮丧。为此,我想与client_id而不是name进行比较。

1 个答案:

答案 0 :(得分:0)

尝试一下。

 let client= {
            "StatusCode": 0,
            "StatusMessage": "OK",
            "StatusDescription": [
                {
                    "client_id": "123",
                    "client_name": "0prVDhbp",
                  },
                {
                    "client_id": "5876856",
                    "client_name": "GKlG1hbp",
                },
                  ....
                {
                    "client_id": "6896868",
                    "client_name": "name",
                }
         ]
      }

    for(let index in client['StatusDescription']){
       console.log(client['StatusDescription'][index]['client_id'])
    }