My goal is to compare two arrays and generate a JSON array marking true if there's a match and false if there isn't. The second array will always have values that match some from the first, and it will be smaller as it's derived from the first array.
let firstArray = ["One", "Two", "Three", "Four", "Five"];
let secondArray = ["Three", "Four"];
let jsonArray = [];
The desired JSON array looks like this:
[
{
"name": "One",
"matched": false
},
{
"name": "Two",
"matched": false
},
{
"name": "Three",
"matched": true
},
{
"name": "Four",
"matched": true
},
{
"name": "Five",
"matched": false
}
]
Usually, I would use a loop like this:
firstArray.forEach(firstElement=>{
secondArray.forEach(secondElement=>{
if(firstArray.indexOf(secondElement)>=0){
jsonArray.push({'name': secondElement, 'matched': true});
}else{
jsonArray.push({'name': secondElement, 'matched': false});
}
});
});
However, this method results in duplicate entries in the JSON array where the name matches but the matched value differs.
I feel like I'm overcomplicating something quite straightforward.