我正在嘗試搜索汽車列表(具有品牌、型號、價格和其他值),并將其與我在表單中搜索的汽車進行比較。我知道如何準確比較兩輛車,但我不知道如何過濾它,例如,我只想按品牌搜索并顯示相同品牌的所有汽車(但不用擔心其他值,因為沒有選擇其他值)。我試過使用 lowdash ._isEqual(),如果兩個對象完全相同,它就會起作用。但是,如果我只想按某個品牌或某個年份或類似的東西搜索,我不知道該怎么做。app.component.tsexport 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.tsexport 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); } }汽車服務.tsexport class CarsService { constructor() {} getAllCars() { return of(Cars); }}
如何在比較另一個對象的同時過濾對象數組?
忽然笑
2021-06-29 09:47:48