JS中如何匹配嵌套数组中的重复值

发布于 2025-01-15 03:04:35 字数 813 浏览 1 评论 0原文

我有一个数组,其中包含动态创建的嵌套数组,它看起来像这样:

[['1', '2'],['1','3', '4'],['1', '3']]

我试图通过仅从这些数组中获取重复值来实现 AND 逻辑。我的预期输出为 ['1'] 因为所有嵌套数组必须包含相同的值。

// [['1', '2'],['1','3', '4'],['1', '3']]
const arrays = [...new Set(response)].filter(newSet => newSet.length > 0);

const builder = []; // array of all the id's no longer needed

// [[],[],[]]
arrays.forEach(arr => {
    // []
    arr.forEach(value => {
        // [[], [], []]
        const found = arrays.filter(a => a.find(v => v === value));

        if (found.length === 0)
            builder.push(value);
        });
});

console.log(builder); // empty []

由于 filter(),这给了我一个空数组。我怎样才能返回所有(在本例中为 3 个,但可能更多)数组包含的值数组?

上述输入的预期输出将为["1"]。任何帮助表示赞赏。

I have an array which contains nested arrays that are dynamically created, it looks like this:

[['1', '2'],['1','3', '4'],['1', '3']]

I am trying to implement AND logic by getting only duplicate values from these arrays. My expected output here would be ['1'] since all nested arrays must contain the same value.

// [['1', '2'],['1','3', '4'],['1', '3']]
const arrays = [...new Set(response)].filter(newSet => newSet.length > 0);

const builder = []; // array of all the id's no longer needed

// [[],[],[]]
arrays.forEach(arr => {
    // []
    arr.forEach(value => {
        // [[], [], []]
        const found = arrays.filter(a => a.find(v => v === value));

        if (found.length === 0)
            builder.push(value);
        });
});

console.log(builder); // empty []

This gives me an empty array because of the filter(). How could I return an array of values that all (3 in this case but could be more) arrays contain?

Expected output with the above input would be ["1"]. Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔嚣张 2025-01-22 03:04:35

据我了解,您需要所有数组中的公共元素

let response1 = [['1', '2'],['1','3', '4'],['1', '3']]
let response2 = [['1', '2', '3'],['1','3', '4'],['1', '3']]


const getCommon = res => [...new Set(res.flat())].filter(a => res.every(c => c.includes(a)));

console.log(getCommon(response1))
console.log(getCommon(response2))

更新
显然,集合转换是不必要的,因为无论如何它必须提供每个数组共有的元素,因此从 res[0] 进行过滤应该可以

let response1 = [['1', '2'],['1','3', '4'],['1', '3']]
let response2 = [['1', '2', '3'],['1','3', '4'],['1', '3']]


const getCommon = res => res[0].filter(a => res.every(c => c.includes(a)));

console.log(getCommon(response1))
console.log(getCommon(response2))

from what I understand you need the common elements from all array

let response1 = [['1', '2'],['1','3', '4'],['1', '3']]
let response2 = [['1', '2', '3'],['1','3', '4'],['1', '3']]


const getCommon = res => [...new Set(res.flat())].filter(a => res.every(c => c.includes(a)));

console.log(getCommon(response1))
console.log(getCommon(response2))

UPDATE
Apparently the set transformation is unnecessary since anyway it has to give the elements common to every array so filtering from res[0] should do

let response1 = [['1', '2'],['1','3', '4'],['1', '3']]
let response2 = [['1', '2', '3'],['1','3', '4'],['1', '3']]


const getCommon = res => res[0].filter(a => res.every(c => c.includes(a)));

console.log(getCommon(response1))
console.log(getCommon(response2))

っ〆星空下的拥抱 2025-01-22 03:04:35

您可以创建一个计数对象,其中包含每个数字的频率,然后仅检查数字的频率是否等于原始数组的长度。

const getIntersectVals = (arrayOfVals)=>{
  const freqs = {};

  for(let arr of arrayOfVals){
    for(let val of arr){
        if(freqs[val]) freqs[val]++;
        else freqs[val] = 1;
    }
  }
  const uniqueVals = Object.keys(freqs);
  const correctVals = uniqueVals.filter(elem=>{
    return freqs[elem] === arrayOfVals.length;
  })
  return correctVals;
}


const arrayOfVals = [['1', '2'],['1','3', '4'],['1', '3']];

console.log(getIntersectVals(arrayOfVals))

You can make a count object that has the frequency of each number in it, and just check if the frequency of a number is equal to the length of the original array.

const getIntersectVals = (arrayOfVals)=>{
  const freqs = {};

  for(let arr of arrayOfVals){
    for(let val of arr){
        if(freqs[val]) freqs[val]++;
        else freqs[val] = 1;
    }
  }
  const uniqueVals = Object.keys(freqs);
  const correctVals = uniqueVals.filter(elem=>{
    return freqs[elem] === arrayOfVals.length;
  })
  return correctVals;
}


const arrayOfVals = [['1', '2'],['1','3', '4'],['1', '3']];

console.log(getIntersectVals(arrayOfVals))

深白境迁sunset 2025-01-22 03:04:35

Lodash intesection 如果你不介意的话

const arrayOfVals = [['1', '2'],['1','3', '4'],['1', '3']];

const result = _.intersection(...arrayOfVals);

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Lodash intesection if you don't mind

const arrayOfVals = [['1', '2'],['1','3', '4'],['1', '3']];

const result = _.intersection(...arrayOfVals);

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文