繁体   English   中英

如何根据每个 object 中的数组从 object 数组中返回某些对象?

[英]How can I return certain objects from object array based on an array inside each object?

例如,如果我想从下面的数组中获取具有“读取”技能的所有对象的新数组,我该怎么做?

我尝试了 testResult 但只是得到了无限循环:(

感谢所有和任何帮助!

const people = [{
    name: "Jon",
    skills: ["reading", "acting", "drinking"],
},
{
    name: "Tim",
    skills: ["rowing", "hockey", "reading"],
},
{
    name: "Lucy",
    skills: ["business", "learning", "hockey"],
},
{
    name: "Michelle",
    skills: ["running", "business", "sleeping"],
},
{
    name: "Michael",
    skills: ["making friends", "surfing"],
}

]

预期收益:

[{
    name: "Jon",
    skills: ["reading", "acting", "drinking"],
},
{
    name: "Tim",
    skills: ["rowing", "hockey", "reading"],
}]

const testResult = testArray.map((obj) => {
obj.skills.map((skill) => {
    if (skill === "reading") {
        setPeople([...people, obj])
    }
})

})

尝试这个

people.filter((e) => e.skills.indexOf('reading') > -1)
var people_new = []; 

for(var i = 0; i < people.length; i++) 
{

   var skills = people[i].skills; 
   
   for(var s = 0; s < skills.length; s++) 
   {
      if( skills[s] == "reading" ) 
      {
         people_new.push(people[i]); 
      }
   }

}
let filterd = [] for(item of people){ if(item.skills.includes('reading')){ filterd.push(item) } } console.log(filtered) // array of objs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM