Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
problem

How to get id from list when text input match or equal items exist on list ?

I work on angular 7


I have input text on nvabar.Component.html write on it part name as following :

HTML
<mat-form-field class="example-full-width">
    <input matInput placeholder="Enter name" [(ngModel)]="partname" >
  </mat-form-field>


I need when write on text input Transistor part then I will search on list parts and get partid =2 .

HTML
navbar.component.ts

export class NavBarComponent implements OnInit {
public parts = [

{
id: 1,
partname: 'hummer',
},
{
id: 2,
partname: 'Transistor',
},
{
id: 3,
partname: 'Air',
}

];
ngOnInit() {
}

So How to get partid from list when textbox text matched items on list ?

What I have tried:

app-routing.module.ts represent routing as following :

const routes: Routes = [

{ path: 'overview', component: OverviewComponent },
{ path: '' , redirectTo: '/overview', pathMatch: 'full'}
Posted
Updated 5-Jan-20 16:52pm
v2

1 solution

You can search an array for an entity by name. Then grab the id

Array.prototype.find() - JavaScript | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900