代码之家  ›  专栏  ›  技术社区  ›  dragonfly

ES6:基于另一个数组的值从数组中获取值

  •  -3
  • dragonfly  · 技术社区  · 5 年前

    enter code here

    DOCUMENTS = [{
          name: 'I551Indicator',
          text: ‘School Document’,
       },
       {
          name: 'I553Indicator',
          text: ‘Birth Certificate’,
       }
    ];
    
    User_Selected_Documents = [{
       I551Indicator: false,
       I553Indicator: true
    }];
    

    DOCUMENTS 数组中,我必须显示值为true的键的文档文本 User_Selected_Documents 数组。

    我试过下面的,似乎得到了文字

    const test = DOCUMENTS.map(doc => doc).map(doc => doc.name).filter(DOCUMENTS.map(selctedDocuments));
    

    User_Selected_Documents.filter(selectedDocument => Object.values(selectedDocument) === true)

    似乎不起作用。

    预期结果:我认为这种情况是正确的 Birth Certificate 自从 I553Indicator true

    1 回复  |  直到 5 年前
        1
  •  1
  •   mukesh210    5 年前

    filter 然后 map

    DOCUMENTS = [{
          name: 'I551Indicator',
          text: 'School Document',
       },
       {
          name: 'I553Indicator',
          text: 'Birth Certificate',
       }
    ];
    
    User_Selected_Documents = [{
       I551Indicator: false,
       I553Indicator: true
    }];
    
    const result = DOCUMENTS.filter(x => User_Selected_Documents[0][x.name]).map(x => x.text)
    
    console.log(result[0])