0

I want to generate a random object which contains a specific id. For example I want to return objects which contains id 3, the first and third one in the example below:

results = [

{title: "A", ids: [2,3,4,]},

{title: "B", ids: [9,4,7]},

{title: "C", ids: [1,3,4]}

]

let data = results[Math.floor(Math.random() * results.length)]

return data

This is what I tried:

 data.map(function (genre){
   let random = genre === 3;
   return random;
})

1 Answer 1

2

You want to filter the results based on the value of data

results.filter(function(x) { return x.ids.includes(data) })
Sign up to request clarification or add additional context in comments.

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.