[英]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.