JS - remove duplicates from array

Im trying to delete dups in this array.

[2018, 2018, 2018]

Open in new window


array created from a for loop -

var years = [];

for(var i = 0; i < response.notes.length; i++){

        var d = new Date(parseInt(response.notes[i]['date'])*1000);
  
        var year = d.getFullYear();
   
        console.log("Year " + year);
          years.push(year);
        }
        

Open in new window





below code is my try at removing the dupes -

 years.push(year);
            sortedYears = years.slice().sort();
            for(var y = 0; y < sortedYears.length - 1; y++){
                if(sortedYears[y + 1] == sortedYears[y]){
                   unqYears.push(sortedYears[i]);
                   }
            }

Open in new window





simply what im trying to do is once the array is generated with years i want to go thought it and remove any years that are the same
Alex LordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
hmm, I would use .filter(), something like:

unqYears= years.filter(function(item, position) {
    return years.indexOf(item) == position;
});

Open in new window

0
Chris StanyonWebDevCommented:
You can filter your array:

var unqYears = years.filter( function(el, index, arr) { return index === arr.indexOf(el) } );

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MishaProgrammerCommented:
            var years = ["2018", "2018", "2018"];
            var uniqueYears = [];
            $.each(years, function (i, el) {
                if ($.inArray(el, uniqueYears) === -1) uniqueYears.push(el);
            });

Open in new window

0
MishaProgrammerCommented:
3 solutions in 1 minute.
0
Alex LordAuthor Commented:
thx Everyone , solved the issue
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
arrays

From novice to tech pro — start learning today.