0

For example if I wanted to get a new array of all objects that had a skill of "reading" from the below array, how would I do this?

I tried testResult but just get infinite loop :(

Thanks for all and any help!

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"],
}

]

Expected return:

[{
    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])
    }
})

})

1
  • Please mark the answer accepted if it helped you. Commented Apr 16, 2021 at 17:19

3 Answers 3

1

try this

people.filter((e) => e.skills.indexOf('reading') > -1)
Sign up to request clarification or add additional context in comments.

Comments

0
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]); 
      }
   }

}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.