用另一个数组过滤数组对象

我想显示在技能数组上过滤的项目,例如,如果我选择“HTML”,则显示项目数组技能中所有带有“HTML”的项目。如果我选择两个技能,则显示具有两个技能的项目。


我的项目有这些数据:


const data = [

  {

    id: "1",

    name: "project1",

    techno: ["JAVASCRIPT", "REACTJS"],

    imageUrl: "link",

  },

  {

    id: "2",

    name: "project2",

    techno: ["HTML", "CSS", "SASS"],

    imageUrl: "link",

  },

  {

    id: "3",

    name: "project3",

    techno: ["JAVASCRIPT", "HTML"],

    imageUrl: "link",

  }

];

还有我的arrayFilter


const filter = ["JAVASCRIPT", "HTML", "CSS"];

目前,我有这个代码:


 data

  .filter((filter) => filter.techno.includes(filter[0]))

  .map(({ id, ...otherProps }) => (

     <ProjectItem key={id} {...otherProps} />

     ))

谢谢您的帮助


慕码人8056858
浏览 109回答 1
1回答

郎朗坤

你可以使用每一个const data = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: '1',&nbsp; &nbsp; &nbsp; &nbsp; name: 'project1',&nbsp; &nbsp; &nbsp; &nbsp; techno: ['JAVASCRIPT', 'REACTJS'],&nbsp; &nbsp; &nbsp; &nbsp; imageUrl: 'link',&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: '2',&nbsp; &nbsp; &nbsp; &nbsp; name: 'project2',&nbsp; &nbsp; &nbsp; &nbsp; techno: ['HTML', 'CSS', 'SASS'],&nbsp; &nbsp; &nbsp; &nbsp; imageUrl: 'link',&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id: '3',&nbsp; &nbsp; &nbsp; &nbsp; name: 'project3',&nbsp; &nbsp; &nbsp; &nbsp; techno: ['JAVASCRIPT', 'HTML', 'REACTJS'],&nbsp; &nbsp; &nbsp; &nbsp; imageUrl: 'link',&nbsp; &nbsp; },];const filter = ['JAVASCRIPT', 'REACTJS'];const result = data.filter(d => filter.every(t => d.techno.includes(t)));console.log(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript