1

I'm a beginner programer and I've a problem with iterate in an array. I want to iterate the same list over an index of it. That is, I want to iterate [j] over [i]. Let [j] finish running and increase [i] I want to iterate the same list over an index of it. That is, I want to iterate [j] over [i]. Let [j] finish going through and increase [i] and do the same thing again until there is nothing else to go through.

for (let i = 0; i < results.length; i++) {
  for (let j = i+1; j < results.length; j++) {
    if (results[i].Email == results[j].Email){
      // delete the item or items from the array
    }
  
  }
  
}

The array in question is this and i want to check if emails are duplicated for delete:

results = [
{Id: 1, Name: "Some", Lastname: "One", Email: "someone@email.com"}, 
{Id: 2, Name: "Some", Lastname: "One", Email: "someone@email.com"}, 
{Id: 3, Name: "Some", Lastname: "One", Email: "someother@email.com"}
]
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
garikoitzz
  • 23
  • 5
  • You would need to show the list you want to do this with. Otherwise it is hard to tell you. – rf1234 Mar 09 '21 at 08:49
  • 1
    please add an example array and the wanted result. – Nina Scholz Mar 09 '21 at 08:52
  • If I understood correctly. What you have shown should do what you want. but start from j =0 instead of i+1. –  Mar 09 '21 at 08:54
  • Read your question again a few times: What you are trying to achieve you seem to be doing in your sample code already. So what exactly is your question? – rf1234 Mar 09 '21 at 08:54
  • @KunwarSagar It was that, but I only delete a duplicate record from the list not all duplicates. – garikoitzz Mar 09 '21 at 09:01
  • what is the problem with the code, beside that you iterate `i` to far, because `j` is then invalid? – Nina Scholz Mar 09 '21 at 09:03
  • @KunwarSagar I think he want do a "comparison like" operation between all the elements and the current one that's why he started from i+1, starting from j=0 would repeat duplicated operation. – moghwan Mar 09 '21 at 09:05
  • @moghwan yes thats it – garikoitzz Mar 09 '21 at 09:08
  • Possible duplicate of [Remove duplicates from an array of objects in JavaScript](https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) ([example](https://jsfiddle.net/4erm27xf/)) – Ivar Mar 09 '21 at 09:18
  • Please note that this is a classic example of an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Instead of asking about the problem you are having (wanting to remove elements from an array of objects with duplicate properties), you are asking about an attempted solution. For future questions on Stack Overflow, make sure to add a bit more context so we can help you better. – Ivar Mar 09 '21 at 09:21
  • @Ivar okay thanks, sorry for that im beginner in this and momently i dont know how its work the plataform. thanks a lot everyone – garikoitzz Mar 09 '21 at 09:27

2 Answers2

1

By deleting an item of an array, you change the length of the array and the positions of indices.

To overcome this, you could iterate from the end with nested loops and check if the same email exist in the lower indices and if so splice the item and move on.

const
    data = [{ Id: 1, Name: "Some", Lastname: "One", Email: "someone@email.com" }, { Id: 2, Name: "Some", Lastname: "One", Email: "someone@email.com" }, { Id: 3, Name: "Some", Lastname: "One", Email: "someother@email.com" }];
    
let i = data.length;

while (i--) {
    let j = i;
    while (j--) {
        if (data[i].Email === data[j].Email) {
            data.splice(i, 1);
            break;
        }
    }
}

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Instead of using nested loops for removing duplicates, you could take an object as reference for seen emails.

const
    data = [{ Id: 1, Name: "Some", Lastname: "One", Email: "someone@email.com" }, { Id: 2, Name: "Some", Lastname: "One", Email: "someone@email.com" }, { Id: 3, Name: "Some", Lastname: "One", Email: "someother@email.com" }],
    seenEmails = {};
    
let i = 0;

while (i < data.length) {
    if (seenEmails[data[i].Email]) {
        data.splice(i, 1);
        continue;
    }
    seenEmails[data[i].Email] = true;
    i++;
}

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can do:

for (let i = 0; i < results.length; i++) {
  for (let j = 0; j < results.length; j++) {
    // this will leave just one entry with the same email
    if (i != j && results[i].Email == results[j].Email){
      results.splice(j, 1);
      j--;
      i--;
    }
  }
}
Attila Szász
  • 707
  • 4
  • 22