如何在多维数组中找到重复值

时间:2015-02-27 09:14:46

标签: javascript arrays sorting object

我在这里遇到麻烦;我想从数组中收集所有重复的值。

修改示例:

Array [
  {
    first: array[ 24, 58],
    last:  array[ 25,     59   ]
  },
  {
    first: array[ 25, 59],
    last:  array[ 23,     57]
  },
  {
    first: array[ 25, 59],
    last:  array[ 26, 54]
  }, 
  {
    first: array[ 23, 58],
    last:  array[ 22,     58]
  }, 
  {
    first: array[ 22, 58],
    last:  array[ 23, 54]
  }, 
  Object, Object, Object, Object, Object, /*98 more…*/ ]

如果脚本找到类似的值,则应将它们推送到另一个数组。

var arrayCollect = []
arrayCollect.push([similar values]);

结果示例

arrayCollect=[[similar values], [similar values]];

和类似的值是类似值([Object, Object...]

的整个集合

我试过这个:

var array =[];
for(var i=0; i < arrayLastFirst.length; i++){
    if(arrayLastFirst[i].first[0] &&
       arrayLastFirst[i].first[1] == 
       arrayLastFirst[i].last[0]  && 
       arrayLastFirst[i].last [1]) {
          array.push(arrayLastFirst[i].first, arrayLastFirst[i].last);
    }
}

但这不正确,我不知道出了什么问题,我怀疑逻辑错误。我修改了我的代码,以便更好地理解我最终想要的东西。 所以我的输出必须是这样的:

Array[
      [
       {
        first: array[ 24, 58],
        last:  array[ 25,59]
        },
        {
        first: array[ 25, 59],//its inside that array couse the first value is the same like the last value before that.
        last:  array[ 23,57]
        },
        {
        first: array[ 25, 59],
        last:  array[ 26, 54]
        }
       ],
       [
        {
        first: array[ 23, 58],
        last:  array[ 22,     58]
        }, 
        {
        first: array[ 22, 58],
        last:  array[ 23, 54]
        }//and these are similar values couse last and the first are the same
       ]
      ]

也许这更容易理解我最终需要什么。

3 个答案:

答案 0 :(得分:0)

我认为你只需要在循环中放一个循环。 所以你抓住你想要比较的第一个并通过整个阵列循环,记住不要与它自己匹配。比你拿第二个并将它与整个数组等进行比较。

for (i = 0; i < arrayLastFirst.length; i++) {
  for (f = arrayLastFirst.length; f++ {
      if( arrayLastFirst[0][i] == arraLastFirst[0][f] || 
           arrayLastFirst[0][i]== arrayLastfirst[0][f] )

}}

编辑:对不起用于堆栈溢出的坏代码仍然没有检查 你可以把i == i放在那里以防止与自身比较

答案 1 :(得分:0)

假设您有这些数据:

var arrayLastFirst = [
    {first: [1, 2], last: [3, 4]},
    {first: [5, 6], last: [6, 7]},
    {first: [0, 8], last: [8, 9]}
];

此测试条件

arrayLastFirst[i].first[0] &&
arrayLastFirst[i].first[1] == arrayLastFirst[i].last[0] &&  
arrayLastFirst[i].last[1]

表示您想要

  • first 的元素0不为空
  • AND last 的元素1不为空
  • AND first 的元素1等于 last 的元素0。

这导致了这个数组:

array == [[5, 6], [6, 7]];

答案 2 :(得分:0)

这样的事情?

var sources = [
  {
    first: [ 23.53521729574, 58.94867816392],
    last:  [ 24.5368636,     59.9483547    ]
  },
  {
    first: [ 24.53521729574, 59.94867816392],
    last:  [ 23.5368636,     58.9483547    ]
  },
  {
    first: [ 23.5368636, 58.9483547],
    last:  [ 23.5368636, 58.9483547]
  }, 
  {
    first: [ 23.53521729574, 58.9483547],
    last:  [ 23.5368636,     58.9483547]
  }, 
  {
    first: [ 23.5368636, 58.9483547],
    last:  [ 23.5368636, 58.9483547]
  }, 
  {
    first: [ 23.53521729574, 58.9483547],
    last:  [ 23.5368636,     58.9483547]
  }, 
  {
    first: [ 23.53521729574, 58.9483547],
    last:  [ 23.5368636,     58.9483547]
  }
];
var results = {};
for(var z in sources) {
    var sz = sources[z];
    if(!results[sz.last]) {
        results[sz.last] = {keys:[],similar:[]}
    }   
    results[sz.last].keys.push(sz); // add all to .keys anything with same .last

}
for(var z in sources) {
    var sz = sources[z];
    var exists = results[sz.first];
    if(exists) {
        exists.similar.push(sz); // add all to .similar anything with same .first
    }
}
console.log(JSON.stringify(results,null,2))

结果:

{
  "24.5368636,59.9483547": {
    "keys": [
      {
        "first": [
          23.53521729574,
          58.94867816392
        ],
        "last": [
          24.5368636,
          59.9483547
        ]
      }
    ],
    "similar": []
  },
  "23.5368636,58.9483547": {
    "keys": [
      {
        "first": [
          24.53521729574,
          59.94867816392
        ],
        "last": [
          23.5368636,
          58.9483547
        ]
      },
      {
        "first": [
          23.5368636,
          58.9483547
        ],
        "last": [
          23.5368636,
          58.9483547
        ]
      },
      {
        "first": [
          23.53521729574,
          58.9483547
        ],
        "last": [
          23.5368636,
          58.9483547
        ]
      },
      {
        "first": [
          23.5368636,
          58.9483547
        ],
        "last": [
          23.5368636,
          58.9483547
        ]
      },
      {
        "first": [
          23.53521729574,
          58.9483547
        ],
        "last": [
          23.5368636,
          58.9483547
        ]
      },
      {
        "first": [
          23.53521729574,
          58.9483547
        ],
        "last": [
          23.5368636,
          58.9483547
        ]
      }
    ],
    "similar": [
      {
        "first": [
          23.5368636,
          58.9483547
        ],
        "last": [
          23.5368636,
          58.9483547
        ]
      },
      {
        "first": [
          23.5368636,
          58.9483547
        ],
        "last": [
          23.5368636,
          58.9483547
        ]
      }
    ]
  }
}