Jonas Olaussen
Jonas Olaussen

Reputation: 79

Return duplicate values in array in Javascript

I am trying to sort out an array and returning the duplicate values. So i know if the value in this case a date exists one or more times. I have been searching and I do only find how to delete the values.

I would like to just return the value that exists one or more times in the array.

This far I have come so long:

var singelbokningar = [];
   $.each(bokadeDatumDone, function(i, el){
   if($.inArray(el, singelbokningar) > -1) singelbokningar.push(el);
});

But it returns a new array without the duplicates.

How should I do?

Upvotes: 0

Views: 123

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219920

Just create another array to hold you duplicates:

var singelbokningar = [];
var duplicates = [];

$.each(bokadeDatumDone, function(i, el) {
   if ( $.inArray(el, singelbokningar) > -1 ) singelbokningar.push(el);
   else duplicates.push(el);
});

Upvotes: 1

Related Questions