【问题标题】:How do I get the difference between two FIFO array states?如何区分两个 FIFO 数组状态?
【发布时间】:2021-11-17 16:57:33
【问题描述】:

我有两个数组代表一个类似 fifo 的状态,一个旧状态和一个新状态。我需要一个函数,通过将新数组与旧数组进行比较来找到新添加的项目。下面是两个数组的 3 个示例,其中 1 与另一个相比在其前面添加了项目:

// Input 1
const arr1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
const arr2 = ['a', 'b', 'a', 'b', 'c', 'd', 'e', 'f', 'g']; // added 'a' and 'b' in front
// Input 2
const arr3 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
const arr4 = ['q', 'r', 'a', 'b', 'c', 'd', 'e', 'f', 'g']; // added 'q' and 'r' in front
// Input 3
const arr5 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
const arr6 = ['a', 'b', 'q', 'a', 'b', 'c', 'd', 'e', 'f']; // added 'a' 'b' and 'q' in front
// New Input 4
const arr7 = ['a', 'b', 'a', 'b', 'c', 'd', 'e', 'f', 'g'];
const arr8 = ['a', 'b', 'a', 'b', 'a', 'b', 'c', 'd', 'e']; // added 'a' and 'b'  in front

请注意,新添加项目的数量从数组的后面移除。 这里需要的功能getItemsAdded(arr1, arr2)函数:

// Desired output for 'getItemsAdded()' function
console.log(getItemsAdded(arr1, arr2)); // [ 'a', 'b' ]
console.log(getItemsAdded(arr3, arr4)); // [ 'q', 'r' ]
console.log(getItemsAdded(arr5, arr6)); // [ 'a', 'b', 'q' ]
// New
console.log(getItemsAdded(arr7, arr8)); // [ 'a', 'b' ]

感觉就像一个简单的问题,但我无法理解它。我无法使用此处提供的解决方案来解决它How to get the difference between two arrays in JavaScript?,因为它是一个不同的问题。

【问题讨论】:

    标签: javascript arrays array-difference


    【解决方案1】:

    代码能说出更多的话,那我的傻解释……

    // Input 1
    const arr1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
    const arr2 = ['a', 'b', 'a', 'b', 'c', 'd', 'e', 'f', 'g']; // added 'a' and 'b' in front
    // Input 2
    const arr3 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
    const arr4 = ['q', 'r', 'a', 'b', 'c', 'd', 'e', 'f', 'g']; // added 'q' and 'r' in front
    // Input 3
    const arr5 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
    const arr6 = ['a', 'b', 'q', 'a', 'b', 'c', 'd', 'e', 'f']; // added 'a' 'b' and 'q' in front
    
    const arr7 = ['a', 'b', 'a', 'b', 'c', 'd', 'e', 'f', 'g'];
    const arr8 = ['a', 'b', 'a', 'b', 'a', 'b', 'c', 'd', 'e']; // added 'a' and 'b'  in front
    
    // Desired output for 'diff()' function
    console.log([...getItemsAdded(arr1, arr2)]); // [ 'a', 'b' ]
    console.log([...getItemsAdded(arr3, arr4)]); // [ 'q', 'r' ]
    console.log([...getItemsAdded(arr5, arr6)]); // [ 'a', 'b', 'q' ]
    console.log([...getItemsAdded(arr7, arr8)]); // [ 'a', 'b' ]
    
    function startsWith(arr1, arr2) {
        for (let i = 0; i < arr1.length; i++)
            if (arr1[i] != arr2[i])
                return false
    
        return true
    }
    function* getItemsAdded(arr1, arr2) {
        while (!startsWith(arr2, arr1)) yield arr2.shift()
    }

    【讨论】:

    • 你是我的英雄,即使其中一个数组为空也会导致正确的输出!这是一种众所周知的方法还是您必须想出什么办法?
    • 我只记得正则表达式是如何工作的,它给了我基本的想法,如何实现它......
    • 我刚刚得到了一个意外的输出,我使用arr2 作为第一个输入,并再次添加了“a”和“b”作为第二个输出。它不是添加的项目,而是将整个新数组作为输出。
    • 我更新了我的答案,在旧答案中,问题是 cmp 函数返回索引,偏移量应该被删除,这个解决方案在这里不起作用,所以我们应该一次删除 1 个元素, 这里我也使用了生成器进行惰性求值。
    • 我正在尝试自己编辑您以前的解决方案,但自己无法弄清楚。再次感谢!现在我只是在getItemsAdded 中添加了一个本地数组,并用arr2.shift() 返回的项目填充它,并返回这个数组而不是生成器。
    猜你喜欢
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    相关资源
    最近更新 更多