-1

Using NodeJS, Express, Passport and a MongoDB database, I'm sorting an array, comparing the current element with the previous one and, if they match, removing it from the array.

But the array of my for each and the 'clean' array aren't the same anymore, and when I have a lot of duplicates, I'm only displaying n-numberOfDuplicates elements on screen.

How could I do this ?

See my code below for already tried solution :

<ul>
    <%var showmovie = false;%>
    <%var occurence = 0;%>
    <%var moviesWatchedArr = currentUser.movieswatched.sort((a,b) => (a.titlewatched > b.titlewatched) ? 1 : ((b.titlewatched > a.titlewatched) ? -1 : 0)); %>

    <%moviesWatchedArr.forEach(function(movie){%>

        <%var indexOfCurrentMovie = moviesWatchedArr.indexOf(movie) - occurence;%>

        <%if (indexOfCurrentMovie > 0 ){%>

            <%var indexOfPrecedentMovie = indexOfCurrentMovie - 1;%>

            <% if(moviesWatchedArr[indexOfCurrentMovie].titlewatched === moviesWatchedArr[indexOfPrecedentMovie].titlewatched){%>

                <% moviesWatchedArr.splice(indexOfCurrentMovie, 1,);%>
                <% occurence += 1;%> //number of time there is the same duplicate
                <% showmovie = false;%>

            <%}else{%>

                <%showmovie = true;%>

            <%}%>
        <%}%>

        <% if(showmovie){%>
            <li><%=moviesWatchedArr[indexOfCurrentMovie].titlewatched%> - <%=moviesWatchedArr[indexOfCurrentMovie].datewatched%>></li>
        <%}%>
    <%})%>
</ul>

Here is the movie array after sorting with duplicates :

[ 
   { 
      "_id":"5d74fd60fd6c7d211a31a3f2",
      "titlewatched":"Attack on Titan",
      "datewatched":"2013"
   },
   { 
      "_id":"5d74db14fc76f5045c0b573c",
      "titlewatched":"Equilibrium",
      "datewatched":"2002"
   },
   { 
      "_id":"5d73f822427fac0ddc4ae6fe",
      "titlewatched":"Harry Potter and the Chamber of Secrets",
      "datewatched":"2002"
   },
   { 
      "_id":"5d750404730dc3024e21b9dc",
      "titlewatched":"Harry Potter and the Deathly Hallows: Part 2",
      "datewatched":"2011"
   },
   { 
      "_id":"5d73fadc6b1a3c10e1db67a8",
      "titlewatched":"Harry Potter and the Deathly Hallows: Part 2",
      "datewatched":"2011"
   },
   { 
      "_id":"5d74fd0efd6c7d211a31a3ee",
      "titlewatched":"Harry Potter and the Deathly Hallows: Part 2",
      "datewatched":"2011"
   },
   { 
      "_id":"5d74fa5c9e791a202130beae",
      "titlewatched":"Harry Potter and the Deathly Hallows: Part 2",
      "datewatched":"2011"
   },
   { 
      "_id":"5d74e52b27f2140e56aada37",
      "titlewatched":"Harry Potter and the Deathly Hallows: Part 2",
      "datewatched":"2011"
   },
   { 
      "_id":"5d73fa2d1c226c102b10f231",
      "titlewatched":"La vie est un singe",
      "datewatched":"2003"
   },
   { 
      "_id":"5d73fa95bdc9a510950f86cc",
      "titlewatched":"La vie est un singe",
      "datewatched":"2003"
   },
   { 
      "_id":"5d73f8c0f6664f0e93275581",
      "titlewatched":"Le coup du singe",
      "datewatched":"1979"
   },
   { 
      "_id":"5d73fb582920d0023fa38a71",
      "titlewatched":"Le coup du singe",
      "datewatched":"1979"
   },
   { 
      "_id":"5d74fd48fd6c7d211a31a3f0",
      "titlewatched":"Spiderman and Grandma",
      "datewatched":"2009"
   },
   { 
      "_id":"5d73f91014e3b80ef8bce755",
      "titlewatched":"Spiderman and Grandma",
      "datewatched":"2009"
   },
   { 
      "_id":"5d74dd8e7b118206cb93bb9b",
      "titlewatched":"Terminator 3: Rise of the Machines",
      "datewatched":"2003"
   },
   { 
      "_id":"5d73fa341c226c102b10f234",
      "titlewatched":"Terminator Salvation",
      "datewatched":"2009"
   }
]

I would like an array without duplicates and a correct display at the end of the for each loop. I'm deleting duplicates by comparing titles (movies).

Danotris
  • 23
  • 3

1 Answers1

0

This might work. You can remove duplicates before sending the data to the template, your business logic must not be within the template.

var arr = [
  {
    id: 1,
    name: 'Rohit'
  },
  {
    id: 1,
    name: 'Rohit'
  },
  {
    id: 2,
    name: 'Akshay'
  },
  {
    id: 3,
    name: 'Someone'
  },
  {
    id: 2,
    name: 'Akshay'
  }
];

const uniqueArray = (arr) => arr.filter((ar,index) => {
  return index === arr.findIndex(obj => {
    return JSON.stringify(obj) === JSON.stringify(ar);
  });
});

console.log(uniqueArray(arr))
Rohit Kashyap
  • 1,553
  • 1
  • 10
  • 16
  • That did the trick ! Thanks ! Also, answers from duplicate really helped me too. Compared to my gigantic function, this seems rather elegant. I'm probably going to explore MDN documentation a little more. – Danotris Sep 08 '19 at 15:38
  • @Danotris mark this as the correct answer if it helped you :) – Rohit Kashyap Sep 08 '19 at 15:49
  • 1
    Yes sorry I'm new to this website, still trying to figure everything out :) – Danotris Sep 08 '19 at 17:11