I'm trying to search through a list of cars (which have a make, model, price, and other values) and compare it to the car I am searching for on my form.
I know how to do it where I compare the two cars exactly, but I'm not sure how to filter it when I, for example, only want to search by make and show all the cars by the same make (but not worry about the other values since nothing else was selected).
I've tried using the lowdash ._isEqual()
, which worked if the two objects are exactly the same. But, I have no idea how to do it if I only want to search by a certain make, or only a certain year, or something like that.
app.component.ts
export class AppComponent {
cars:Car[];
title = 'car-dealership';
constructor(private carsService:CarsService) {}
searchCars(car:Car) {
this.carsService.getAllCars().subscribe(resp => {
this.cars = resp;
this.cars.filter(c => {
//do something
})
})
}
}
input-form.component.ts
export class InputFormComponent implements OnInit {
@Output() searchCars: EventEmitter<any> = new EventEmitter();
make:string;
year:number;
color:string;
sunRoof = false;
fourWheel = false;
lowMiles = false;
powerWindows = false;
navigation = false;
heatedSeats = false;
price:number;
constructor() { }
ngOnInit() {
}
onSubmit() {
const selectedCar = {
color: this.color,
hasHeatedSeats: this.heatedSeats,
hasLowMiles: this.lowMiles,
hasNavigation: this.navigation,
hasPowerWindows: this.powerWindows,
hasSunroof: this.sunRoof,
isFourWheelDrive: this.fourWheel,
make: this.make,
price: Number(this.price),
year: Number(this.year)
}
console.log('form submitted with:', selectedCar);
this.searchCars.emit(selectedCar);
}
}
cars.service.ts
export class CarsService {
constructor() {}
getAllCars() {
return of(Cars);
}
}