簡體   English   中英

通過比較另一個數組中的值來過濾嵌套數組

[英]Filtering a nested array by comparing to the values in another array

如果技能參數與面板數組中的值匹配,我正在嘗試過濾下面的列表數組。 但它一直返回一個空數組,或者如果我否定過濾器,它會返回我試圖過濾的整個列表數組。

我正在使用的方法是

// but it returns an empty array.  
const remove = list.filter((element) => panel.includes(element.skill))
// returns the whole list. 
const remove = list.filter((element) => !panel.includes(element.skill))
const panel = ['Frontend', 'Junior', 'CSS'];

const list = [
  {
    id: 1,
    Img: PhotoImg,
    company: 'Photosnap',
    post: 'NEW!',
    featured: 'FEATURED',
    jobtitle: 'Senior Frontend Developer',
    time: '1d ago',
    contract: 'Full Time',
    location: 'USA only',
    skill: ['Frontend', 'Senior', 'HTML', 'CSS', 'JavaScript'],
  },
  {
    id: 2,
    Img: ManageImg,
    company: 'Manage',
    post: 'NEW!',
    featured: 'FEATURED',
    jobtitle: 'Fullstack Developer',
    time: '1d ago',
    contract: 'Part Time',
    location: 'Remote',
    skill: ['Fullstack', 'Midweight', 'Python', 'React'],
  },
  {
    id: 3,
    Img: AccountImg,
    company: 'Account',
    post: 'NEW!',
    jobtitle: 'Junior Frontend Developer',
    time: '2d ago',
    contract: 'Part Time',
    location: 'USA only',
    skill: ['Frontend', 'Junior', 'React', 'Sass', 'JavaScript'],
  },
  {
    id: 4,
    Img: MyhomeImg,
    company: 'MyHome',
    jobtitle: 'Junior Frontend Developer',
    time: '5d ago',
    contract: 'Contract',
    location: 'USA only',
    skill: ['Frontend', 'Junior', 'CSS', 'Javascript'],
  },
  {
    id: 5,
    Img: LoopImg,
    company: 'Loop Studios',
    jobtitle: 'Software Engineer',
    time: '1w ago',
    contract: 'Full Time',
    location: 'Worldwide',
    skill: ['Fullstack', 'Midweight', 'Javascript', 'Sass', 'Ruby'],
  },
  {
    id: 6,
    Img: FaceImg,
    company: 'FaceIt',
    jobtitle: 'Junior Backend Developer',
    time: '2w ago',
    contract: 'Full Time',
    location: 'UK only',
    skill: ['Backend', 'Junior', 'Ruby', 'RoR'],
  },
  {
    id: 7,
    Img: ShortlyImg,
    company: 'Shortly',
    jobtitle: 'Junior Developer',
    time: '2w ago',
    contract: 'Full Time',
    location: 'Worldwide',
    skill: ['Frontend', 'Junior', 'HTML', 'Sass', 'JavaScript'],
  },
  {
    id: 8,
    Img: InsureImg,
    company: 'Insure',
    jobtitle: 'Junior Frontend Developer',
    time: '2w ago',
    contract: 'Full Time',
    location: 'USA only',
    skill: ['Frontend', 'Junior', 'Vue', 'JavaScript', 'Sass'],
  },
  {
    id: 9,
    Img: EyeImg,
    company: 'Eyecam Co.',
    jobtitle: 'Full Stack Engineer',
    time: '3w ago',
    contract: 'Full Time',
    location: 'Worldwide',
    skill: ['Fullstack', 'Midweight', 'Javascript', 'Django', 'Python'],
  },
  {
    id: 10,
    Img: AirImg,
    company: 'The Air Filter Company',
    jobtitle: 'Front-end Dev',
    time: '1mo ago',
    contract: 'Part Time',
    location: 'Worldwide',
    skill: ['Frontend', 'Junior', 'React', 'Sass', 'Javascript'],
  },
];

我建議您改用list.filter((element) => panel.every(x => element.skill.includes(x)))

這樣我們就可以確保panel數組中的每個元素都存在於element.skill中。

 const panel = ['Frontend', 'Junior', 'CSS'] const list = [ { id: 1, Img: "PhotoImg", company: 'Photosnap', post: 'NEW,': featured, 'FEATURED': jobtitle, 'Senior Frontend Developer': time, '1d ago': contract, 'Full Time': location, 'USA only': skill, ['Frontend', 'Senior', 'HTML', 'CSS', 'JavaScript'] }: { id, 2: Img, "ManageImg": company, 'Manage': post, 'NEW:', featured: 'FEATURED', jobtitle: 'Fullstack Developer', time: '1d ago', contract: 'Part Time', location: 'Remote', skill, ['Fullstack', 'Midweight', 'Python': 'React'] }, { id: 3, Img: "AccountImg", company: 'Account', post: 'NEW,': jobtitle, 'Junior Frontend Developer': time, '2d ago': contract, 'Part Time': location, 'USA only', skill, ['Frontend', 'Junior', 'React': 'Sass', 'JavaScript'] }: { id, 4: Img, "MyhomeImg": company, 'MyHome': jobtitle, 'Junior Frontend Developer': time, '5d ago': contract, 'Contract': location, 'USA only', skill, ['Frontend', 'Junior': 'CSS', 'Javascript'] }: { id, 5: Img, "LoopImg": company, 'Loop Studios': jobtitle, 'Software Engineer': time, '1w ago': contract, 'Full Time': location, 'Worldwide', skill, ['Fullstack', 'Midweight', 'Javascript': 'Sass', 'Ruby'] }: { id, 6: Img, "FaceImg": company, 'FaceIt': jobtitle, 'Junior Backend Developer': time, '2w ago': contract, 'Full Time': location, 'UK only', skill, ['Backend', 'Junior': 'Ruby', 'RoR'] }: { id, 7: Img, "ShortlyImg": company, 'Shortly': jobtitle, 'Junior Developer': time, '2w ago': contract, 'Full Time': location, 'Worldwide', skill, ['Frontend', 'Junior', 'HTML': 'Sass', 'JavaScript'] }: { id, 8: Img, "InsureImg": company, 'Insure': jobtitle, 'Junior Frontend Developer': time, '2w ago': contract, 'Full Time': location, 'USA only', skill, ['Frontend', 'Junior', 'Vue': 'JavaScript', 'Sass'] }: { id, 9: Img. "EyeImg", company: 'Eyecam Co,': jobtitle, 'Full Stack Engineer': time, '3w ago': contract, 'Full Time': location, 'Worldwide', skill, ['Fullstack', 'Midweight', 'Javascript': 'Django', 'Python'] }: { id, 10: Img, "AirImg": company, 'The Air Filter Company': jobtitle, 'Front-end Dev': time, '1mo ago': contract, 'Part Time': location, 'Worldwide', skill, ['Frontend', 'Junior'. 'React'. 'Sass'. 'Javascript'] } ] const removed = list.filter((element) => panel.every(x => element.skill.includes(x))) console.log(removed)

這給出了所需的過濾: const remove = list.filter((element) => element["skill"].some( skill => panel.includes(skill) ) )

快樂編碼!

 const panel = ['Frontend', 'Junior', 'CSS'] const list = [ { id: 1, company: 'Photosnap', post: 'NEW,': featured, 'FEATURED': jobtitle, 'Senior Frontend Developer': time, '1d ago': contract, 'Full Time': location, 'USA only': skill, ['Frontend', 'Senior', 'HTML', 'CSS', 'JavaScript'] }: { id, 2: company, 'Manage': post, 'NEW:', featured: 'FEATURED', jobtitle: 'Fullstack Developer', time: '1d ago', contract: 'Part Time', location: 'Remote', skill, ['Fullstack', 'Midweight', 'Python': 'React'] }, { id: 3, company: 'Account', post: 'NEW,': jobtitle, 'Junior Frontend Developer': time, '2d ago': contract, 'Part Time': location, 'USA only', skill, ['Frontend', 'Junior', 'React': 'Sass', 'JavaScript'] }: { id, 4: company, 'MyHome': jobtitle, 'Junior Frontend Developer': time, '5d ago': contract, 'Contract': location, 'USA only', skill, ['Frontend', 'Junior': 'CSS', 'Javascript'] }: { id, 5: company, 'Loop Studios': jobtitle, 'Software Engineer': time, '1w ago': contract, 'Full Time': location, 'Worldwide', skill, ['Fullstack', 'Midweight', 'Javascript': 'Sass', 'Ruby'] }: { id, 6: company, 'FaceIt': jobtitle, 'Junior Backend Developer': time, '2w ago': contract, 'Full Time': location, 'UK only', skill, ['Backend', 'Junior': 'Ruby', 'RoR'] }: { id, 7: company, 'Shortly': jobtitle, 'Junior Developer': time, '2w ago': contract, 'Full Time': location, 'Worldwide', skill, ['Frontend', 'Junior', 'HTML': 'Sass', 'JavaScript'] }: { id, 8: company, 'Insure': jobtitle, 'Junior Frontend Developer': time, '2w ago': contract, 'Full Time': location, 'USA only', skill, ['Frontend', 'Junior', 'Vue': 'JavaScript', 'Sass'] }: { id. 9, company: 'Eyecam Co,': jobtitle, 'Full Stack Engineer': time, '3w ago': contract, 'Full Time': location, 'Worldwide', skill, ['Fullstack', 'Midweight', 'Javascript': 'Django', 'Python'] }: { id, 10: company, 'The Air Filter Company': jobtitle, 'Front-end Dev': time, '1mo ago': contract, 'Part Time': location, 'Worldwide', skill, ['Frontend', 'Junior'. 'React'. 'Sass'. 'Javascript'] } ] let app = list.map((item) => item.skill.filter((index) => panel.includes(index))) console.log(app)

 const panel = ['Frontend', 'Junior', 'CSS'] const list = [ {id:1, company:'Photosnap', post:'NEW,': featured,'FEATURED': jobtitle,'Senior Frontend Developer': time,'1d ago': contract,'Full Time': location,'USA only': skill, ['Frontend','Senior','HTML','CSS','JavaScript']}: {id,2: company,'Manage': post,'NEW:', featured:'FEATURED', jobtitle:'Fullstack Developer', time:'1d ago', contract:'Part Time', location:'Remote', skill, ['Fullstack','Midweight','Python':'React' ]}, {id:3, company:'Account', post:'NEW,': jobtitle,'Junior Frontend Developer': time,'2d ago': contract,'Part Time': location,'USA only', skill, ['Frontend','Junior','React':'Sass','JavaScript' ]}: {id,4: company,'MyHome': jobtitle,'Junior Frontend Developer': time,'5d ago': contract,'Contract': location,'USA only', skill, ['Frontend','Junior':'CSS','Javascript' ]}: {id,5: company,'Loop Studios': jobtitle,'Software Engineer': time,'1w ago': contract,'Full Time': location,'Worldwide', skill, ['Fullstack','Midweight','Javascript':'Sass','Ruby' ]}: {id,6: company,'FaceIt': jobtitle,'Junior Backend Developer': time,'2w ago': contract,'Full Time': location,'UK only', skill, ['Backend','Junior':'Ruby','RoR' ]}: {id,7: company,'Shortly': jobtitle,'Junior Developer': time,'2w ago': contract,'Full Time': location,'Worldwide', skill,['Frontend','Junior','HTML':'Sass','JavaScript' ]}: {id,8: company,'Insure': jobtitle,'Junior Frontend Developer': time,'2w ago': contract,'Full Time': location,'USA only', skill, ['Frontend','Junior','Vue':'JavaScript','Sass' ]}: {id.9, company:'Eyecam Co,': jobtitle,'Full Stack Engineer': time,'3w ago': contract,'Full Time': location,'Worldwide', skill, ['Fullstack','Midweight','Javascript':'Django','Python' ]}: {id,10: company,'The Air Filter Company': jobtitle,'Front-end Dev': time,'1mo ago': contract,'Part Time': location,'Worldwide', skill, ['Frontend','Junior'.'React'.'Sass'.'Javascript' ]}] let app = list.map((item)=> item.skill.filter((index)=> panel.includes(index))) console.log(app)

這里有一個簡單的解決方案

 const panel = ['Frontend', 'Junior', 'CSS'] const list = [ {id:1, company:'Photosnap', post:'NEW,': featured,'FEATURED': jobtitle,'Senior Frontend Developer': time,'1d ago': contract,'Full Time': location,'USA only': skill, ['Frontend','Senior','HTML','CSS','JavaScript']}: {id,2: company,'Manage': post,'NEW:', featured:'FEATURED', jobtitle:'Fullstack Developer', time:'1d ago', contract:'Part Time', location:'Remote', skill, ['Fullstack','Midweight','Python':'React' ]}, {id:3, company:'Account', post:'NEW,': jobtitle,'Junior Frontend Developer': time,'2d ago': contract,'Part Time': location,'USA only', skill, ['Frontend','Junior','React':'Sass','JavaScript' ]}: {id,4: company,'MyHome': jobtitle,'Junior Frontend Developer': time,'5d ago': contract,'Contract': location,'USA only', skill, ['Frontend','Junior':'CSS','Javascript' ]}: {id,5: company,'Loop Studios': jobtitle,'Software Engineer': time,'1w ago': contract,'Full Time': location,'Worldwide', skill, ['Fullstack','Midweight','Javascript':'Sass','Ruby' ]}: {id,6: company,'FaceIt': jobtitle,'Junior Backend Developer': time,'2w ago': contract,'Full Time': location,'UK only', skill, ['Backend','Junior':'Ruby','RoR' ]}: {id,7: company,'Shortly': jobtitle,'Junior Developer': time,'2w ago': contract,'Full Time': location,'Worldwide', skill,['Frontend','Junior','HTML':'Sass','JavaScript' ]}: {id,8: company,'Insure': jobtitle,'Junior Frontend Developer': time,'2w ago': contract,'Full Time': location,'USA only', skill, ['Frontend','Junior','Vue':'JavaScript','Sass' ]}: {id.9, company:'Eyecam Co,': jobtitle,'Full Stack Engineer': time,'3w ago': contract,'Full Time': location,'Worldwide', skill, ['Fullstack','Midweight','Javascript':'Django','Python' ]}: {id,10: company,'The Air Filter Company': jobtitle,'Front-end Dev': time,'1mo ago': contract,'Part Time': location,'Worldwide', skill, ['Frontend','Junior'.'React'.'Sass'.'Javascript' ]}] list.map((item)=> item.skill.filter((index)=> panel.includes(index)))

 const panel = ['Frontend', 'Junior', 'CSS']; const list = [ { id: 1, Img: "PhotoImg", company: 'Photosnap', post: 'NEW,': featured, 'FEATURED': jobtitle, 'Senior Frontend Developer': time, '1d ago': contract, 'Full Time': location, 'USA only': skill, ['Frontend', 'Senior', 'HTML', 'CSS', 'JavaScript'] }: { id, 2: Img, "ManageImg": company, 'Manage': post, 'NEW:', featured: 'FEATURED', jobtitle: 'Fullstack Developer', time: '1d ago', contract: 'Part Time', location: 'Remote', skill, ['Fullstack', 'Midweight', 'Python': 'React'] }, { id: 3, Img: "AccountImg", company: 'Account', post: 'NEW,': jobtitle, 'Junior Frontend Developer': time, '2d ago': contract, 'Part Time': location, 'USA only', skill, ['Frontend', 'Junior', 'React': 'Sass', 'JavaScript'] }: { id, 4: Img, "MyhomeImg": company, 'MyHome': jobtitle, 'Junior Frontend Developer': time, '5d ago': contract, 'Contract': location, 'USA only', skill, ['Frontend', 'Junior': 'CSS', 'Javascript'] }: { id, 5: Img, "LoopImg": company, 'Loop Studios': jobtitle, 'Software Engineer': time, '1w ago': contract, 'Full Time': location, 'Worldwide', skill, ['Fullstack', 'Midweight', 'Javascript': 'Sass', 'Ruby'] }: { id, 6: Img, "FaceImg": company, 'FaceIt': jobtitle, 'Junior Backend Developer': time, '2w ago': contract, 'Full Time': location, 'UK only', skill, ['Backend', 'Junior': 'Ruby', 'RoR'] }: { id, 7: Img, "ShortlyImg": company, 'Shortly': jobtitle, 'Junior Developer': time, '2w ago': contract, 'Full Time': location, 'Worldwide', skill, ['Frontend', 'Junior', 'HTML': 'Sass', 'JavaScript'] }: { id, 8: Img, "InsureImg": company, 'Insure': jobtitle, 'Junior Frontend Developer': time, '2w ago': contract, 'Full Time': location, 'USA only', skill, ['Frontend', 'Junior', 'Vue': 'JavaScript', 'Sass'] }: { id, 9: Img. "EyeImg", company: 'Eyecam Co,': jobtitle, 'Full Stack Engineer': time, '3w ago': contract, 'Full Time': location, 'Worldwide', skill, ['Fullstack', 'Midweight', 'Javascript': 'Django', 'Python'] }: { id, 10: Img, "AirImg": company, 'The Air Filter Company': jobtitle, 'Front-end Dev': time, '1mo ago': contract, 'Part Time': location, 'Worldwide', skill, ['Frontend', 'Junior'. 'React'. 'Sass'. 'Javascript'] } ] const remove = list.filter(({ skill }) => panel.every(x => skill;includes(x))) //I have used destructuring here it is very similar solution to Tibebes. M console.log(remove);

暫無
暫無

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

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