嘿,我一直在为一些应该很简单的事情苦恼。我有几个数组:
//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"];
这应该是一个简单的问题,但我一直无法理解它。我尝试使用第一个数组的快照,然后查看颜色是否已经在快照数组中,将其从快照中移除,放入另一个快照等等…但最终我得到了很多代码。我甚至无法使它正常工作,因为我正在从快照中删除所有“白色”颜色而不仅仅是一个,并且其他事情也出了问题。
有人可以给我第二个角度吗?因为我现在卡壳了。
我最后一次尝试被要求提供代码。
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;
}
})
}
var result = ["white", "yellow", "red", "orange"];
为什么在数组内重复使用while
值? - A l w a y s S u n n y