0

Hi I am getting below responses from 2 api calls. First I'm calling 1st api and getting below respose

first response I'm getting from 1st api call

dataName =   [
{
"id": "1",
"name": "stage1",
},
{
"id": "2",
"name": "stage2",
},
{
"id": "3",
"name": "stage3",
}
]

In dropdown i'm display name values

 <mat-select (selectionChange)="onOptionsSelected($event)"required placeholder="Choose Data Name">
              <mat-option *ngFor="let obj of dataName" >
                {{obj.name}}
              </mat-option>
            </mat-select>

On selection of name from first dropdown I need to call a different api which will give me below response

cluName = [
  {   
"cname": "cname1",
"id": "3",
 },
 {   
"cname": "cname3",
"id": "2",
 },
 {   
"cname": "cname1",
"id": "2",
}
]

On dropdown selection when selecting name I need to send the ID instead of name to match the cname associated with the ID and display in another dropdown.

Suppose if I select stage2 then I need to send ID :2 and match in second response its associated cname(cname3,cname1) and display those cname in second dropdwon. How to achieve this please help.

On selectionChange I tried following method

onOptionsSelected(event) {
 this.newArrayCluster = [];
 this.cluName.forEach(element => {
 this.newArrayCluster.push(element.find(item => item['id'] === 
event.value))});
console.log(this.newArrayCluster); 
 }

But I'm getting error "element.find is not a function".

4
  • What have you tried so far? Also in the response from first call the property is name whereas in the HTML template it's datacenter-name. Which is the right name? Also if the object property has an hyphen -, it's better to access it using bracket notation obj['datacenter-name'] rather than dot notation: stackoverflow.com/a/7122629/6513921 Commented Jul 12, 2021 at 7:51
  • Yes i edited the code its name Commented Jul 12, 2021 at 7:56
  • I have updated the code with the method I tried so far Commented Jul 12, 2021 at 8:16
  • I can't explain the code right now, but try to gather info from this Stackblitz implementation. Hit up here if you have any question. Commented Jul 12, 2021 at 8:56

2 Answers 2

2

you must do like this in your Html file

 <mat-select  (selectionChange)="onOptionsSelected($event)"required placeholder="Choose Data Name">
              <mat-option [value]="obj.id " *ngFor="let obj of dataName" >
                {{obj.datacenter-name}}
              </mat-option>
            </mat-select>
Sign up to request clarification or add additional context in comments.

2 Comments

But how I will iterate the second array and get the matching value of cname with the id?
You can also use let dataArr = dataName.filter(data => data.id == value )
0

*please follow given below link it would help ful for you first you should make relation between two array *

https://stackblitz.com/edit/angular-qvqqgc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.