我正在尝试搜索汽车列表(具有品牌、型号、价格和其他值),并将其与我在表单中搜索的汽车进行比较。
我知道如何准确比较两辆车,但我不知道如何过滤它,例如,我只想按品牌搜索并显示相同品牌的所有汽车(但不用担心其他值,因为没有选择其他值)。
我试过使用 lowdash ._isEqual(),如果两个对象完全相同,它就会起作用。但是,如果我只想按某个品牌或某个年份或类似的东西搜索,我不知道该怎么做。
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
})
})
}
}
输入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);
}
}
汽车服务.ts
export class CarsService {
constructor() {}
getAllCars() {
return of(Cars);
}
}
跃然一笑
烙印99
相关分类