比较两个 arrays 的值，如果一个值被删除，则只返回差值，而不是添加

[英]Comparing the value of two arrays and only returning the difference if a value is removed, not added

I am working on a project with an array of values that could possibly change every 30 seconds.我正在开发一个项目，其中包含一组可能每 30 秒更改一次的值。 I need to compare the first `array` to the current `array` of values every time this runs and return the difference between the values ONLY if a value is removed.每次运行时，我都需要将第一个`array`与当前`array`的值进行比较，并且仅当删除了一个值时才返回值之间的差异。

For example if `array1 = [12, 14, 16]` and `array2 = [12, 16, 18]` , then I need to return `14` and do something with it.例如，如果`array1 = [12, 14, 16]``array2 = [12, 16, 18]` ，那么我需要返回`14`并对其进行处理。 What would be the best way to accomplish this?实现这一目标的最佳方法是什么？

This is my current code, but it returns any difference between the two arrays:这是我当前的代码，但它返回两个 arrays 之间的任何差异：

``````function diffArr (array1, array2) {
const temp = [];
array1 = array1.toString().split(',').map(Number);
array2 = array2.toString().split(',').map(Number);

for (var i in array1) {
if(!array2.includes(array1[i])) temp.push(array1[i]);
}

for(i in array2) {
if(!array1.includes(array2[i])) temp.push(array2[i]);
}

return temp.sort((a,b) => a-b);
}
``````

Try this:试试这个：

``````
const array1 = [12, 14, 16, 20, 10]

const array2 = [12, 16, 18]

const diffArr = (array1, array2) => {
const temp = array1.filter(ele => !array2.includes(ele))
return temp.sort((a,b) => a-b);
}

console.log(diffArr(array1, array2)) // output = [ 10, 14, 20 ]

``````