I need to iterate through an array of IDs called "world". The idea is that if the ID value in world exists in myArray[n].id, then I want to remove the entire element from myArray. If it doesn't exist, then I want to add it to myArray.
world = ["12424126","12461667","12492468","12761163"]
myArray = [
{"id": "12424126"},
{"id": "12761163"},
{"id": "12492468"}
]
For example, if the first element in world[n] ("12424126") matches { "id": "12424126" } in myArray, I should delete the element { "id": "12424126" }
If the first element in world[n] ("12424126") does not exist in myArray:
myArray.push ({ "id": world[n] });
}
for (let n = 0; n <= world.length; n++) {
let ID = world[n];
finished = false;
if (myArray.find(x => x.id === ID)) {
let index = _.findIndex(myArray, { "id": ID });
if (index > -1) { myArray.splice(index, 1);
finished = true;}
}
if (!finished) // ISSUE: RECORD GETS ADDED REGARDLESS OF FINISHED TRUE/FALSE
{myArray.push ({ id: ID }); // HOW CAN I SOLVE THIS PROBLEM?
}
}