Find if two arrays are repeated in array and then select them

后端 未结 6 860
后悔当初
后悔当初 2020-12-03 18:51

I have multiple arrays in a main/parent array like this:

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];
<
6条回答
  •  误落风尘
    2020-12-03 19:05

    You could also do this with a single Array.reduce where you would only push to a result property if the length is equal to 3:

    var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];
    
    console.log(array.reduce((r,c) => {
      let key = c.join('-')
      r[key] = (r[key] || 0) + 1
      r[key] == 3 ? r.result.push(c) : 0  // if we have a hit push to result
      return r
    }, { result: []}).result)             // print the result property

提交回复
热议问题