Probably making a silly mistake, but i can't seem to figure this one out.
Based on an existing array of strings, i want to check if they exist as an object value within my array of objects. If true, push them into the new array with a true value, if false, also push them in the new array, but with a false value.
Example of my code so far:
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}
]
This pushes objects to my new array, and it all works fine! Problem is when i want to add the false ones. I'm trying to do this as followed:
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}
]
// would expect it to be:
[
{id:12, author: 'A', correct: true},
{id:4, author: 'B', correct: false},
{id:16, author: 'C', correct: true}
]
What am i missing here?