查找是否在数组中重复了两个数组,然后选择它们

Tim*_*alk 6 javascript arrays jquery object multidimensional-array

我在主/父数组中有多个数组,如下所示:

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];
Run Code Online (Sandbox Code Playgroud)

这是用于简化阅读的数组:

[1, 17]
[1, 17]
[1, 17]
[2, 12]
[5, 9]
[2, 12]
[6, 2]
[2, 12]
[2, 12]
Run Code Online (Sandbox Code Playgroud)

我想选择重复3次或更多次(> 3)的数组并将其分配给变量.所以在这个例子中,var repeatedArrays将是[1, 17][2, 12].

所以这应该是最终的结果:

console.log(repeatedArrays);
>>> [[1, 17], [2, 12]]
Run Code Online (Sandbox Code Playgroud)

我在这里发现了类似的东西,但它使用了underscore.js和lodash.

我怎么能用javascript甚至jquery(如果需要)?

Nin*_*olz 10

你可以使用Map带字符串的数组并计数,然后按计数过滤并恢复数组.

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]],
    result = Array
        .from(array.reduce(
            (map, array) =>
                (json => map.set(json, (map.get(json) || 0) + 1))
                (JSON.stringify(array)),
            new Map
         ))
        .filter(([, count]) => count > 2)
        .map(([json]) => JSON.parse(json));
        
console.log(result);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)

使用想要计数的地图过滤.

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]],
    result = array.filter(
        (map => a => 
            (json =>
                (count => map.set(json, count) && !(2 - count))
                (1 + map.get(json) || 1)
            )
            (JSON.stringify(a))
        )
        (new Map)
    );
        
console.log(result);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)

独特!

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]],
    result = array.filter(
        (s => a => (j => !s.has(j) && s.add(j))(JSON.stringify(a)))
        (new Set)
    );
        
console.log(result);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)


Kam*_*ski 9

试试这个

array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))
Run Code Online (Sandbox Code Playgroud)

时间复杂度O(n)(一个数组通过滤波器函数).灵感来自Nitish的答案.

说明

(r={}, a=>...)逗号之后将返回最后一个表达式(这是a=>...)(例如(5,6)==6).在r={}我们设置一个临时对象,我们将存储唯一键.在过滤功能a=>...a我们有当前array元素.在r[a]JS中隐含地转换a为字符串(例如1,17).然后在!(2-(r[a]=++r[a]|0))我们增加发生元素的计数器a并且如果元素a发生3次则返回true(作为过滤器函数值).如果r[a]未定义则++r[a]返回NaN,并进一步NaN|0=0(也number|0=number).该r[a]=INITIALISE第一计数器的值,如果我们忽略它的 ++将只设置NaNr[a]这是不可递增的(所以我们需要把零在INIT).如果我们删除2-结果,我们得到没有重复的输入数组 - 或者我们也可以得到它a=>!(r[a]=a in r).如果我们改为2-,1-我们只得到重复的数组.

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

var r= array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))

console.log(JSON.stringify(r));
Run Code Online (Sandbox Code Playgroud)

  • 有些人真的很喜欢挑战极限。尊重。二) (2认同)

Nit*_*ang 5

您可以使用Object.reduce,Object.entries如下所示

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];


let res = Object.entries(
            array.reduce((o, d) => {
              let key = d.join('-')
              o[key] = (o[key] || 0) + 1

              return o
          }, {}))
          .flatMap(([k, v]) => v > 2 ? [k.split('-').map(Number)] : [])
  
  
console.log(res)
Run Code Online (Sandbox Code Playgroud)

或者可能只是Array.filters

var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

let temp = {}
let res = array.filter(d => {
  let key = d.join('-')
  temp[key] = (temp[key] || 0) + 1
  
  return temp[key] == 3
})

console.log(res)
Run Code Online (Sandbox Code Playgroud)