Seems like I'm missing something obvious, but I can't seem to crack this one.
I have an existing array of strings and a separate array of objects. My goal is to check if the strings exist as values in the objects within my array of objects. If they do, I want to push them into a new array with a true value. If not, I still want to push them into the new array, but with a false value.
Here's a snippet of my code:
const answers = [12, 3, 16]
const quotes = [
{ id: 12, author: 'A'},
{ id: 4, author: 'B'},
{ id: 16, author: 'C'},
]
let checkedQuotes = [];
answers.forEach((answer) => {
quotes.find((quote) => (quote.id === answer
&& checkedQuotes.push({
id: quote.id,
author: quote.author,
correct: true,
})
));
});
returns => [
{id:12, author: 'A', correct: true},
{id:16, author: 'C', correct: true}
]
The code above successfully pushes the correct objects to my new array. However, I encounter issues when trying to handle the false ones. I've attempted the following approach:
answers.forEach((answer) => {
quotes.find((quote) => (quote.id === answer
? checkedQuotes.push({
id: quote.id,
author: quote.author,
correct: true,
})
: checkedQuotes.push({
id: quote.id,
author: quote.author,
correct: false,
})
));
});
returns => [
{id:12, author: 'A', correct: true},
{id:12, author: 'A', correct: false},
{id:12, author: 'A', correct: false}
]
// The expected output should be:
[
{id:12, author: 'A', correct: true},
{id:4, author: 'B', correct: false},
{id:16, author: 'C', correct: true}
]
What am I overlooking here?