简体   繁体   English

奇怪的打cup从阵列中删除重复项

[英]Weird hiccup removing duplicates from an array

I'm running into a weird glitch. 我遇到了一个奇怪的小故障。 I have a bit of code where I'm running thru an array of arrays, grabbing a bunch of city names and concatenating them all together. 我有一些代码可以通过数组运行,获取一堆城市名称并将它们连接在一起。 I need to remove the duplicates from the finished list. 我需要从完成的列表中删除重复项。 This should be pretty simple. 这应该很简单。 Use a count to figure out which city has more than one instance and then splice them out. 使用计数来找出哪个城市有多个实例,然后将它们拼接起来。 My returned array isn't coming out right though and I'm not sure why. 我返回的数组虽然没有正确显示,但我不确定为什么。 Can anyone spot what I'm doing wrong? 谁能发现我在做什么错?

const input = [
{
    name: "ACH2000",
    year: 2005,
    cities: ['Chicago', 'New York', 'Ames', 'Columbus'],
    ages: [12, 32, 2, 51]
},
{
    name: "FXG3000",
    year: 2008,
    cities: ['Chicago', 'Joliet', 'Plymouth', 'Dallas'],
    ages: [12, 32, 2, 51]
},
{
    name: "GTG1234",
    year: 2012,
    cities: ['Indy', 'Tampa', 'Houston', 'Dallas'],
    ages: [12, 32, 2, 51]
}
];

function getUniqueCities(data){
  let citiesInArray = data.map(function(item){ return item.cities });
  let concatCities = [].concat.apply([], citiesInArray);
  let count = {};
  for(let i = 0; i< concatCities.length; i++) {
      let num = concatCities[i];
      count[num] = count[num] ? count[num]+1 : 1;
      if(count[num] > 1){
        console.log('bad',num);
        concatCities.splice(num, 1);
      } else {
        console.log('good',num);
      }
  }
  console.log(count);
  console.log(concatCities);
}

getUniqueCities(input);

you can try something like this 你可以尝试这样的事情

var input = [
    {
        name: "ACH2000",
        year: 2005,
        cities: ['Chicago', 'New York', 'Ames', 'Columbus'],
        ages: [12, 32, 2, 51]
    },
    {
        name: "FXG3000",
        year: 2008,
        cities: ['Chicago', 'Joliet', 'Plymouth', 'Dallas'],
        ages: [12, 32, 2, 51]
    },
    {
        name: "GTG1234",
        year: 2012,
        cities: ['Indy', 'Tampa', 'Houston', 'Dallas'],
        ages: [12, 32, 2, 51]
    }
];

var citiesStats = {};

input.forEach(data =>
    data.cities.forEach(city => {
        if (!citiesStats[city]) {
            citiesStats[city] = 0;
        }
        ++citiesStats[city];
    })
);

var cities = Object.keys(citiesStats);

// ["Chicago", "New York", "Ames", "Columbus", "Joliet", "Plymouth", "Dallas", "Indy", "Tampa", "Houston"]
console.log(cities);
// {"Chicago":2,"New York":1,"Ames":1,"Columbus":1,"Joliet":1,"Plymouth":1,"Dallas":2,"Indy":1,"Tampa":1,"Houston":1}
console.log(citiesStats);

As nnnnnn suggested splicing inside the loop is messing up the indices in the array. 正如nnnnnn所建议的那样,在循环内部进行拼接会弄乱数组中的索引。

If you can use Set , here is a solution: 如果可以使用Set ,这是一个解决方案:

Array.from(new Set(concatCities))

Here is a link to fiddle . 这是小提琴的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM