找出两个数组之间的对称差异

Finding the symmetric difference between two arrays

我需要找到元素中两个数组之间的任何差异,并将所述元素推入一个新数组,然后在最后返回该数组。我通过搜索从该网站提取了一个函数,其预期目的是计算元素在数组中出现的次数并返回它。首先,我将两个数组连接在一起,然后应用此函数(对其进行修改以尽可能适应我的问题)。然后我尝试将不同的元素(没有出现两次)推送到新数组。我的代码当然不起作用,而且我也是 Javascript 的新手,所以请放轻松。

下面是我试过的一些代码,没有通过任何测试:

function diffArray(arr1, arr2) {
  var newArr = [];

  let tempArr = arr1.concat(arr2);

  function countInArray(array, what) {
    var count = 0;
    for (var i = 0; i < array.length; i++) {
        if (array[i] === what) {
            count++;
        }
    }
    if (countInArray(tempArr, tempArr[i]) < 2) {
      newArr.push(tempArr[i]);
    } 
}


  return newArr;
}

如果您提供任何代码,请尝试将其分解给我,以便我更好地理解和学习。

反转第二个数组,这可能会减轻您的任务

Array.Reverse方法: http://www.w3schools.com/jsref/jsref_reverse.asp

你可以拿Set

The Set object lets you store unique values of any type, whether primitive values or object references.

和return左边和右边的区别。

function getSymDifference(a, b) {
    return getDifference(a, b).concat(getDifference(b, a));
}

function getDifference(a, b) {
    var setB = new Set(b);
    return a.filter(v => !setB.has(v));
}

console.log(getSymDifference(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"])); // ["pink wool"]
console.log(getSymDifference([1, "calf", 3, "piglet"], [7, "filly"])); // [1, "calf", 3, "piglet", 7, "filly"]
console.log(getSymDifference([], ["snuffleupagus", "cookie monster", "elmo"]));
console.log(getSymDifference([1, 2, 3, 5], [1, 2, 3, 4, 5]));

经典的拼接数组的方法,防止使用已经访问过或搜索过的项目再次使用。

function getSymDifference(a, b) {
    var aa = a.slice(),
        bb = b.slice(),
        result = [],
        i, j;

    for (i = 0; i < aa.length; i++) {
        j = bb.indexOf(aa[i]);
        if (j === -1) {
            result.push(aa[i]);
        } else {
            bb.splice(j, 1);
        }
    }
    return result.concat(bb);
}

console.log(getSymDifference(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"])); // ["pink wool"]
console.log(getSymDifference([1, "calf", 3, "piglet"], [7, "filly"])); // [1, "calf", 3, "piglet", 7, "filly"]
console.log(getSymDifference([], ["snuffleupagus", "cookie monster", "elmo"]));
console.log(getSymDifference([1, 2, 3, 5], [1, 2, 3, 4, 5]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

要获得预期结果,请通过对代码进行少量更改来使用以下选项

  1. Return count for countInArray(目前它 returns 未定义)

function countInArray(array, what) { var count = 0; for (var i = 0; i < array.length; i++) { if (array[i] === what) { count++; } } return count }

  1. 删除在 countInArray 方法中调用 countInArray
  2. 运行 For Loop for tempArr 与其他每个tempArr值进行比较

for (var j = 0; j < tempArr.length; j++) { if (countInArray(tempArr, tempArr[j]) < 2) { newArr.push(tempArr[j]); } }

工作代码:

function diffArray(arr1, arr2) {
  let tempArr = arr1.concat(arr2);
  let newArr = [];
  function countInArray(array, what) {
    var count = 0;
    for (var i = 0; i < array.length; i++) {
        if (array[i] === what) {
            count++;
        }
    }
    return count
}
  
    for (var j = 0; j < tempArr.length; j++) {
       if (countInArray(tempArr, tempArr[j]) < 2) {
         newArr.push(tempArr[j]);
       } 
    }


  return newArr;
}


let arr1 = ["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"]
let arr2 = ["diorite", "andesite", "grass", "dirt", "dead shrub"]
console.log(diffArray(arr1, arr2))

let arr3 = [1, "calf", 3, "piglet"]
let arr4 =  [7, "filly"]
console.log(diffArray(arr3, arr4))

codepen - https://codepen.io/nagasai/pen/OYNdZX?editors=1010

我能找到的最优雅的解决方案显然是使用 ES7 声明的。我将在下面 post,但如果有人可以帮助我弄清楚如何使我的初始代码正常工作,我们将不胜感激。

function diffArray(arr1, arr2) {

let difference = arr1
                 .filter(x => !arr2.includes(x))
                 .concat(arr2.filter(x => !arr1.includes(x)));


  return difference;
}

基本上,这会根据第二个数组中没有的内容来过滤第一个数组,并根据第一个数组中没有的内容连接过滤后的第二个数组。所以它得到了双方,而不仅仅是一方,因此 'symmetric'。