Find and return all duplicated values within an array using Javascript

Currently, I am facing a challenge with sorting out an array and identifying duplicate values. My goal is to pinpoint the occurrences of a specific value, like a date, within the array. Most of my search results have focused on deleting duplicates, but I simply want to extract the duplicated value itself.

So far, I have made some progress:


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

Unfortunately, this code generates a new array that lacks any duplicate entries. How can I modify this to achieve my desired result?

Answer №1

To manage duplicate values, you can create an additional array:

var singleBookings = [];
var duplicates = [];

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experiencing trouble with the integration of react native Vector icons in a current project

I'm encountering an issue with React Native Vector Icons. The error message I'm receiving says: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You l ...

Adding elements to an array in Node.js

In my 'both' object, I need to store 2 arrays: eng and fr. Each of these arrays contains multiple objects. How can I transfer all values from frDisplayData to fr:[] within the 'both' object: const displayData = []; var both = {eng:dis ...

What are the steps for generating and implementing shared feature files in Cucumber?

I am currently utilizing Cucumber to define some tests for the project I am working on, but as the application grows larger, I find myself in need of a more efficient structure. project | feature_files | | app1.js | | app2.js | | app3.js ...

Unique title: "Tailored confirmation dialogue box"

I am looking to customize the confirmation message using plugins. In my PHP table records, there is a delete button in each row. How can I personalize the confirmation pop-up similar to the jQuery plugin mentioned below? <?php echo' <tr class=" ...

Maximizing the functionality of a datetime picker with dual validators

I have successfully implemented a Date time picker on my website. Everything is functioning properly, but I am looking to apply two validators: one to disable Saturday and Sunday, and the other to exclude US holidays. Below is the function I am currently u ...

Issue with manipulating currency conversion data

Currently, I am embarking on a project to develop a currency conversion application resembling the one found on Google's platform. The main hurdle I am facing lies in restructuring the data obtained from fixer.io to achieve a similar conversion method ...

The response from a Fetch API POST request comes back as a blank text

When I use fetch() to send a post request, the response is coming back empty. Here is my code: JS: async getTotalCompletionTimes() { var res = await fetch("repository/maps.php?method=getcompletiontimes&map="+this.getName(), {method: 'POST&ap ...

Typescript is throwing an error with code TS2571, indicating that the object is of type 'unknown'

Hey there, I'm reaching out for assistance in resolving a specific error that has cropped up. try{ } catch { let errMsg; if (error.code === 11000) { errMsg = Object.keys(error.keyValue)[0] + "Already exists"; } return res.status ...

What is the process for sorting Google Map markers with AngularJS?

.controller('MapCtrl', ['$scope', '$http', '$location', '$window', '$filter', '$ionicLoading', '$compile','$timeout','$ionicPopup', function ...

What is the best way to retrieve the current CSS width of a Vue component within a flexbox layout grid after it has been modified?

There's something about this Vue lifecycle that has me scratching my head. Let me simplify it as best I can. I've got a custom button component whose size is controlled by a flex grid container setup like this: <template> < ...

Issue with Canvas loading in Firefox

Can you help me identify the issue with this code snippet? window.LoadImage = function(el, canvasId){ var canvas = document.getElementById(canvasId); var context = canvas.getContext("2d"); var dialogCanvas = document.ge ...

Creating dynamic and engaging animations for components using ReactCSSTransitionGroup

I'm currently attempting to add animation to a modal that appears when a button is clicked using ReactCSSTransitionGroup. The modal is showing up on button click, however, there is no transition effect. My render method is set up like this: render() ...

Broadcasting events across the entire system

I'm trying to accomplish something specific in Angular2 - emitting a custom event globally and having multiple components listen to it, not just following the parent-child pattern. Within my event source component, I have: export class EventSourceCo ...

Creating a multi-tiered dropdown menu in the navigation bar with the help of Bootstrap 4 and Angular 7

My goal is to implement a multilevel dropdown using bootstrap 4 and angular 7. While I successfully created a simple dropdown in the navbar following the official bootstrap documentation, I struggled to make the multilevel dropdown work. After referring ...

Navigating through JSON data in PHP without prior knowledge of the data's length

I'm currently working on retrieving all the images from an image API that has a limitation of returning up to 500 results at a time. If there is a next_page field in the result, I need to extract its value and append it to the URL. The process should ...

Executing an AJAX request in the onsubmit event of a form results in the form submission proceeding even when return false is

I am facing an issue with a form that I intend to use only for an AJAX call. The AJAX call is triggered on submit in order to utilize the auto-check feature for required fields. Despite returning false on submit, the form still submits. Surprisingly, when ...

Steps to create a personalized material-ui element

I am looking to create a custom time duration component by modifying the TextField component. https://i.stack.imgur.com/fLsFs.png https://i.stack.imgur.com/SdpdH.png If anyone has any advice or assistance, it would be greatly appreciated. Thank you! ...

The use of callbacks is ineffective in addressing the asynchronous nature of

Hello everyone, I'm currently working on a weather app and I'm facing an issue with the asynchronous behavior of useState. I've come across some suggestions on Stack Overflow that using a callback in the useState function might solve the pro ...

Form an item using an array

Is there a way to efficiently convert this array into a map? Here is how the array looks: var array = [{ "id" : 123 }, { "id" : 456 }, { "id" : 789 }]; The desired output should be: var result = { "123": { id: 123 } , "456": { id: 456 } , ...

JavaScript - analyzing multiple arrays against a single array

Can anyone help me determine whether 'buns' or 'duns' has the most elements in common with 'me'? I need a method to accomplish this task. var buns = ['bap', 'bun', 'bop']; var duns = ['dap&a ...