Hey I'm breaking my head over something that should be very simple. I have a few arrays
//input
var array1 = ["white", "white", "yellow"];
var array2 = ["white", "white", "yellow", "red"];
var array3 = ["white", "white", "yellow", "orange"];
//desired output
var result = ["white", "white", "yellow", "red", "orange"];
This should be a simple problem but I just haven't been able to wind my head around it. I tried by using a snapshot of the first array, then see if the color was already in the snapshot array remove it from the snapshot, put it in another snapshot etc... but I ended up with lines and lines of code. Couldn't even get it to work since I was deleting all "white" colors from the snapshot instead of just one and other things where going wrong.
Cans someone give me a second perspective, cause I'm running against a wall atm
My last attempt as asked to provide the code
let attacks = entry.attacks;
if(attacks !== undefined){
let lastSnapshot = [];
attacks.forEach(attack => {
if(lastSnapshot.length === 0){
attack.forEach(attackColor => {
lastSnapshot.push(attackColor)
})
}else{
let newSnapshot = [];
attack.forEach(attackColor => {
var start_index = lastSnapshot.findIndex(attackColor)
if(start_index !== -1){
var number_of_elements_to_remove = 1;
lastSnapshot.splice(start_index, number_of_elements_to_remove);
}
newSnapshot.push(attackColor)
})
lastSnapshot = newSnapshot;
}
})
}