簡體   English   中英

Angular 10:如何比較兩個 arrays

[英]Angular 10:how to compare two arrays

我有兩個 arrays,一個帶有 colors,一個帶有類別。

 "colors": [ { "buttonId": "color-cinder-spark-red", "filmCategory": 1, "name": "cinder spark red", "previewImg": "assets/filmPreviewImg/gloss-cinder-spark-red.png", "default": true }, { "buttonId": "color-gloss-red-metallic", "filmCategory": 2, "name": "red metallic", "previewImg": "assets/filmPreviewImg/gloss-red-metallic.png" }, { "buttonId": "color-dragon-red", "filmCategory": 3, "name": "dragon red", "previewImg": "assets/filmPreviewImg/gloss-dragon-red.png" },...

數組非常大。

這里是分類:

 "types": [ { "id": 1, "name": "Gloss", "type": "gloss" }, { "id": 2, "name": "Matte", "type": "matte", "default": true }, { "id": 3, "name": "Satin", "type": "satin" },...

在 colors 的數組中,類別顯示為 id。 我需要將所選顏色的id與類別進行比較,然后獲取類別的名稱。 在這里我將獲取所選顏色的類別 id

  filmTypes = filmTypes.types;
  filmColors = filmColors.colors;

  currentColor: any;
 
  modalRef: BsModalRef;
  constructor(private modalService: BsModalService,
              private _productService: ProductService) {}

  ngOnInit(): void {
    const defaultColor = this.filmColors.find((c) => c.default);
    this.selectColor(defaultColor);  
       
    this._productService.currentFilmColor$.subscribe((color: any) => {      
      this.currentColor = color.filmCategory;          
      console.log('color2',this.currentColor);            
    });    
  }

如何在另一個數組中比較這個 id 並獲取類別名稱?

使用Array#find

const { name } = filmTypes.find(({ id }) => id === this.currentColor);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM