提问者:小点点

Angular 10:如何比较两个阵列


我有两个数组,一个有颜色,另一个有类别

null

"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"
        },...

null

阵列非常大。

以下是分类:

null

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

null

在颜色数组中,类别显示为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并获得类别名称?


共1个答案

匿名用户

使用array#find

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