-2

I've read most of questions similar to this, but still, I don't understand how to make .find() work in my app.

I have a service with API:

export class VehicleService {
  private defUrl = 'API';

  constructor(private http: Http) { }

  getVehicleByVehicleId(vehicleid?: any) {
    const url = (!vehicleid) ? this.defUrl : 'API1&vehicle_id=' + vehicleid;
    return this.http.get(url)
      .map(res => res.json());
  }


    searchVehicleId(description?: string) {
    const url = (!description) ? this.defUrl : 'API2&description=' + description;
    return this.http.get(url)
      .map(res => res.json().vehicles);
  }
}

Component

export class VehicleComponent implements OnInit {

  ngOnInit() {

    this.searchQuery = new FormGroup({
      vehicleDescription: new FormControl('', Validators.required)
    });
  }


  constructor(private vehicleService: VehicleService) { }

  public fleet: Fleet[];
  public description: Description[];
  public searchQuery: FormGroup;
  public searchVehicleId: any;


  public searchByVehicleId(searchQuery) {
    this.vehicleService
      .searchVehicleId(searchQuery.value.vehicleDescription)
      .subscribe(searchQuery => {
        this.description = searchQuery;
        this.searchVehicleId = this.description.find() // <= HOW TO IMPLEMENT THIS?
    this.vehicleService
      .getVehicleByVehicleId(this.searchVehicleId)
      .subscribe(searchQuery => {
        this.vehicle = searchQuery;
      })
      });
  }
interface Vehicle {
  status: number;
  dallases: Vehicle[];
}

interface VehicleDetails {
  vehicle_id: number;
  dallassettings: string;
  dallasupdated: string;
  dallas_list: DallasList[];
}

interface DallasList {
  number: number;
  auth: number;
}

//////////////////////////////////
//////////////////////////////////

interface Description {
  vehicles: DescriptionDetails[];
}

interface DescriptionDetails {
  id: number;
  custom_description: string;
  description: string;
}

API2

{
    "vehicles": [{
        "description": "SKODA ROOMSTER",
        "id": 123456,
        "custom_description": ""
    }, {
        "description": "SKODA ROOMSTER",
        "id": 123456789,
        "custom_description": ""
    }]
}

So, what I do here is: pass description inside FormGroup - vehicleDescription field to searchByVehicleId method. Here I want to find id of passed vehicleDescription velue (it's "description" field in API2). When we find the id, we use it to further in this.vehicleService.getVehicleByVehicleId...

Main problem is - I have no idea, how to properly implement .find().

Thanks

Haseoh
  • 910
  • 3
  • 18
  • 38
  • 1
    https://stackoverflow.com/a/35398031/4826457 – Suraj Rao Jun 06 '17 at 08:41
  • Solved: `this.searchVehicleId = this.description.find(x => x.id === x.id).id.toString();`. Thanks for help, if I found this question earlier, there would be no question from my side. – Haseoh Jun 06 '17 at 09:11

1 Answers1

2

The MDN docs:

this.searchVehicleId = this.description.find(x => x.id === someId);
Laoujin
  • 9,962
  • 7
  • 42
  • 69