一个函数,用于获取任意给定数量的包含字符串的数组的交集元素

时间:2019-07-08 08:27:14

标签: javascript ecmascript-6

我具有以下函数,用于返回某些字符串数组之间的元素的交集。

最初它只打算处理2个数组,但是我开始需要处理两个以上的数组,因此我添加了条件return以使其递归。

如何使其具有足够的灵活性,使其能够处理任意数量的数组(当然等于或大于2的数组)。

我虽然打算使用...rest参数,但是我还不知道该怎么做。

function intersection(list1, list2, list3, list4) {
  const result = [];

  for (let i = 0; i < list1.length; i++) {
      let item1 = list1[i];
      let found = false;
      for (var j = 0; j < list2.length && !found; j++) {
          found = item1 === list2[j];
      }
      if (found === true) {
          result.push(item1);
      }
  }
  if (list3 && list4) {
    return intersection(result,list3,list4);
  }
  if (list3) {
    return intersection(result,list3);
  }
  return result;
}

SNIPPET

function intersection(list1, list2, list3, list4) {
  const result = [];

  for (let i = 0; i < list1.length; i++) {
      let item1 = list1[i];
      let found = false;
      for (var j = 0; j < list2.length && !found; j++) {
          found = item1 === list2[j];
      }
      if (found === true) {
          result.push(item1);
      }
  }
  if (list3 && list4) {
    return intersection(result,list3,list4);
  }
  if (list3) {
    return intersection(result,list3);
  }
  return result;
}

const x1 = ['a','b','c','d','e'];
const x2 = ['a','b','c','d'];
const x3 = ['a','b','c'];
const x4 = ['a','b'];

console.log('Intersection(x1,x2,x3,x4): ' + JSON.stringify(intersection(x1,x2,x3,x4)));
console.log('Intersection(x1,x2,x3): ' + JSON.stringify(intersection(x1,x2,x3)));
console.log('Intersection(x1,x2): ' + JSON.stringify(intersection(x1,x2)));

3 个答案:

答案 0 :(得分:3)

无需递归,您可以使用rest parameters ...并为交集添加Set并对其进行过滤。

function intersection(...arrays) {
    return arrays.reduce((a, b) => a.filter(Set.prototype.has, new Set(b)));
}

console.log(intersection(['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c'], ['a', 'b']));
console.log(intersection(['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c']));
console.log(intersection(['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd']));

具有递归的版本

function intersection(a, b = [], ...arrays) {
    var i = a.filter(Set.prototype.has, new Set(b));
    return arrays.length
         ? intersection(i, ...arrays)
         : i;
}

console.log(intersection(['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c'], ['a', 'b']));
console.log(intersection(['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c']));
console.log(intersection(['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd']));

答案 1 :(得分:1)

Nina的答案当然更大,但我也会根据您自己的代码提出解决方案。


由于您的函数至少需要两个列表,因此请将它们放在参数列表中。然后,对兼职使用销毁。

然后,如果至少有一个辅助列表,则通过将第二个参数指定为第一个辅助参数来重新执行您的功能。使用Array#shift将其从列表中删除。不要忘了列出您的新教师名单。

function intersection(list1, list2, ...lists) {
  const result = [];

  for (let i = 0; i < list1.length; i++) {
      let item1 = list1[i];
      let found = false;
      for (var j = 0; j < list2.length && !found; j++) {
          found = item1 === list2[j];
      }
      if (found === true) {
          result.push(item1);
      }
  }
  
  if (lists.length) {
    return intersection(result, lists.shift(), ...lists);
  }
  
  return result;
}

const x1 = ['a','b','c','d','e'];
const x2 = ['a','b','c','d'];
const x3 = ['a','b','c'];
const x4 = ['a','b'];

console.log('Intersection(x1,x2,x3,x4): ' + JSON.stringify(intersection(x1,x2,x3,x4)));
console.log('Intersection(x1,x2,x3): ' + JSON.stringify(intersection(x1,x2,x3)));
console.log('Intersection(x1,x2): ' + JSON.stringify(intersection(x1,x2)));

答案 2 :(得分:0)

您可以创建另一个函数,该函数比较两个数组并返回交集。在intersection函数中,使用Rest parameters作为称为lists的2D数组传递数组。使用reducecompare作为回调与数组相交

function compare(list1, list2) {
  const result = [];

  for (let i = 0; i < list1.length; i++) {
      let item1 = list1[i];
      let found = false;
      for (var j = 0; j < list2.length && !found; j++) {
          found = item1 === list2[j];
      }
      if (found === true) {
          result.push(item1);
      }
  }

  return result
}

function intersection(...lists) {
  return lists.reduce(compare)
}

const x1 = ['a','b','c','d','e'];
const x2 = ['a','b','c','d'];
const x3 = ['a','b','c'];
const x4 = ['a','b'];

console.log('Intersection(x1,x2,x3,x4): ' + JSON.stringify(intersection(x1,x2,x3,x4)));
console.log('Intersection(x1,x2,x3): ' + JSON.stringify(intersection(x1,x2,x3)));
console.log('Intersection(x1,x2): ' + JSON.stringify(intersection(x1,x2)));

注意:您可能可以简化compare的实现。这只是为了用您现有的代码进行演示