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

cbdeveloper

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

最初它只打算处理2个数组,但是我开始需要它处理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)));

妮娜·斯科茨(Nina Scholz)

无需递归,您可以使用rest参数...并获取交集aSet并对其进行过滤。

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']));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从包含字符串的第一个元素到包含另一个元素的最后一个元素获取数组的切片

返回一个数组,该数组包含的数组中的元素数量小于或等于给定数组中的元素的数量

从字符串中的给定数组中找到一个单词

写一个函数来计算给定字符串中回文子字符串的数量?

给定一个字符串数组,返回仅包含回文的字符串数组

检查字符串是否包含任意数量的数字,后跟一个特定的字母[Python]

字符串拆分返回一个包含两个元素而不是一个元素的数组

从给定的字符串中获取一个单词

是否有一个函数可以过滤包含字符串的行,但在选定的列上包含R中包含给定字符串的名称?

如果对象中的一个元素包含字符串,则 Javascript 从数组中删除对象

函数返回包含子字符串所有元素的第一个可能的子字符串

给定一个只包含 1 和 0 的字符串,返回 1 大于 0 的子字符串的数量

实现一个C函数,该函数在给定字符上拆分字符串,并在拆分后返回字符串数组(以及数组长度)

使用lodash检查数组元素是否包含另一个数组元素的子字符串

Java填充数组,包含给定字符串字符(?)的N个元素

如何获取字符串数组列表的最后一个元素?

获取拆分字符串数组的最后一个元素

从第一个元素中获取特定字符串-数组

给定campgrounds数组,编写一个函数,该函数返回总和和匹配的输入字符串

Java字符串-为给定的字符串创建一个char数组

函数检查一个字符串是否包含另一个字符串的所有元素

获取拆分字符串的最后一个元素

分割字符串并获取最后一个元素

编写一个函数,删除给定对象的任何属性,这些属性的值是比给定数字长的字符串,并返回该对象

将多个(任意数量)spark DataFrame列连接为一个“ |” 分隔字符串

给定一个任意字符串,我如何将命令打包为bash?

查找要替换为给定数字的数组中元素的数量,以使该数组的总和小于另一个给定数字

查找数组中最常见的字符串失败,包含2个元素或相等的数量

创建一个从字符串生成器派生的类以追加任意数量的字符串