If statement 如何使用javascript比较/过滤2个数组中的值,并将唯一值推送到数组末尾

If statement 如何使用javascript比较/过滤2个数组中的值,并将唯一值推送到数组末尾,if-statement,filter,iteration,If Statement,Filter,Iteration,我有这段代码,我试图通过循环两个数组并使用IF条件来比较数组。我觉得我的循环和IF语句不正确。无论如何,我想要的是将RED的值从newWords推送到oldWords数组中,这样oldWords就等于['TEST','EAR','RED]。我希望保持这种顺序,因为基本上每次输入新值时,我都希望将它们附加到我的oldWords数组的末尾 let oldWords = ['TEST', 'EAR']; let newWords = ['TEST', 'RED', 'EAR']; function

我有这段代码,我试图通过循环两个数组并使用IF条件来比较数组。我觉得我的循环和IF语句不正确。无论如何,我想要的是将
RED
的值从
newWords
推送到
oldWords
数组中,这样
oldWords
就等于
['TEST','EAR','RED]
。我希望保持这种顺序,因为基本上每次输入新值时,我都希望将它们附加到我的
oldWords
数组的末尾

let oldWords = ['TEST', 'EAR'];
let newWords = ['TEST', 'RED', 'EAR'];

function compareArr() {

    for (const oldWord of oldWords) {
        for (const newWord of newWords) {
            if (newWord !== oldWord) {
                oldWords.push(newWord)
            } 
        }
    }
}

compareArr();

// I would like to insert the word RED from newWords to the array of oldWords.
// I want the output of oldWords to be ['TEST', 'EAR', 'RED'] , it is important that RED is at the end.