Is it possible to remove multiple keysets from Maps using Javascript?

Hi, I'm working on a map that contains key-value pairs. How can I remove multiple keysets from my map?

Here is the part where the map is created:

// The result key looks something like 0-119, 0-110, 0-118 for each entry
this.OptionsMap.set('' + result, selectedList[index]);

And here is the part where keys are deleted from the map:

const entriesd = JSON.parse(JSON.stringify(selectedList));
for (let [key, value] of Object.entries(entriesd)) {
   if (value.key === 'AB') {
      console.log('keys', value.index)
      this.OptionsMap.delete('' + value.index);
   }
}

The selectedList contains the following elements:

(3) [{…}, {…}, {…}]
0: {id: "101", value: "A1B", key: "AB", index: "0-119", …}
1: {id: "130", value: "130", key: "AB", index: "0-110", …}
2: {id: "130", value: "130", key: "AB", index: "0-118", …}

While I am able to retrieve the indices in my console.log as 0-119, 0-110, 0-118, I need assistance with removing multiple indices in the deletion process. Any suggestions would be greatly appreciated. Thank you in advance.

Answer №1

What do you think of this?

// Here is a practical example to illustrate a point
var chosen = [
  {id: "101", value: "A1B", key: "AA", index: "0-119"},
  {id: "130", value: "130", key: "AB", index: "0-110"},
  {id: "130", value: "130", key: "AB", index: "0-118"}
];
var optionsMap = new Map([
  ["0-119", {id: "101", value: "A1B", key: "AA", index: "0-119"}],
  ["0-110", {id: "130", value: "130", key: "AB", index: "0-110"}],
  ["0-118", {id: "130", value: "130", key: "AB", index: "0-118"}],
  ["0-117", {id: "135", value: "131", key: "AB", index: "0-117"}],
  ["0-116", {id: "136", value: "132", key: "AB", index: "0-116"}],

]);
console.log('optionsMap.size',optionsMap.size);

// Filter out specific items from the optionsMap based on the 'key' property
chosen.filter(item => item.key === 'AB').forEach(el => optionsMap.delete(el.index));

// Check and log the final size of the optionsMap after filtering
console.log('optionsMap.size',optionsMap.size);

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

Encountering difficulties with managing the submit button within a ReactJS form

As I work on creating a User registration form using React JS, I encounter an issue where the console does not log "Hello World" after clicking the submit button. Despite defining the fields, validations, and the submit handler, the functionality seems to ...

Handlers for adjustments not correctly updating values in introduced object

I am facing an issue with a table that displays data fetched from an API and a select dropdown. Whenever a checkbox is selected, the name key along with its value is added to an array object named selectedFields. checkbox = ({ name, isChecked }) => { ...

I'm puzzled as to why my object is showing null during the initial render and passing null data via react router[v6]. Can anyone

Wondering about an issue with sending user data to the profile page using React Router v6? Despite populating the currentUser state based on conditions (logged in, user found, user not found), the data in that state doesn't seem to get sent to the Pro ...

Steps for generating a JSON

I have created a JSON Object similar to the example below, but I am unsure of how to populate the parameters field using JSONStringer. Should the parameters field be a JSONArray or an Array of Strings? {"name":"Katy", "parameters":["JAK","1999"], "Age":2 ...

Are there any alternatives to ui-ace specifically designed for Angular 2?

I am currently working on an Angular2 project and I'm looking to display my JSON data in an editor. Previously, while working with AngularJS, I was able to achieve this using ui-ace. Here is an example of how I did it: <textarea ui-ace="{ us ...

Limiting the style of an input element

How can I mask the input field within an <input type="text" /> tag to restrict the user to a specific format of [].[], with any number of characters allowed between the brackets? For example: "[Analysis].[Analysis]" or another instance: "[Analysi ...

Utilizing Lodash in TypeScript to merge various arrays into one cohesive array while executing computations on each individual element

I am working with a Record<string, number[][]> and attempting to perform calculations on these values. Here is an example input: const input1 = { key1: [ [2002, 10], [2003, 50], ], }; const input2 = { key ...

Having trouble converting the value into Vue2Editor

I'm currently utilizing vue2editor and encountering an issue on the edit page where I am attempting to pass the current content value to the vue2editor. Despite my efforts, the vue2editor still displays blank content as if no value was passed. Below ...

Can anyone recommend a super sleek drop-down navigation menu that includes both images and text descriptions?

My interest has been piqued on how to create a menu similar to the one on with all the images and text details. Does anyone have any insights on the technologies utilized for this? Are there any commercial options available that offer a similar functiona ...

What is the process of setting up an array and populating it with characters from a buffer

In my code, there is a method that looks like this: int create_nodes(Source* source, int maxTokens) { int nodeCount = 0; Token* p_Tstart = source->tknBuffer; Token* p_Tcurrent = source->tknBuffer; while ((p_Tcurrent - p_Tstart) < ...

Error: The reference to Excel is not recognized

I am looking to develop an Office add-in using angularjs and angularjs-ui-router: <bt:Urls> <bt:Url id="Contoso.Taskpane3.Url" DefaultValue="https://localhost:3000/addin/test" /> </bt:Urls> The module name is app, and th ...

Struggling to send the correct array in response to an HTTP request. Is there a way to efficiently send results from a function using expressjs?

Issue Summary: Currently, I am facing a challenge while working on a software program that relies on an SQL database at the backend and utilizes the npm SQLite3 module for communication purposes. The problem arose when performing an HTTP request to the ser ...

How can I call the telerik radgrid.databind() function using a JavaScript function?

Currently, I am coding in ASP .NET and have an ASPX page featuring a Telerik RadGrid. I am curious to know if it is feasible to call the RadGrid.DataBind() method from within a JavaScript function? ...

Discovering nearby data in an array of objects using MongoDB

My goal is to extract a specific object from an array within a database document by using the wildcards $near and $geometry. The model I am working with looks like this: const schema = new mongoose.Schema({ name: {...}, email: {...}, ... locations: [ ...

Using "this" in callback functions in JS references the current context

Part 1: Is there a way to reference a parent property dynamically from within a callback? Consider the following scenario: var someClass = { htmlClass : 'aFunClass', bindEvents: function(){ jQuery(function(){ alert( th ...

How to identify NaN in JavaScript code?

I am attempting to use JavaScript to determine the number of days in a selected month of a selected year. However, I keep receiving a NaN value as a result. How can I resolve this issue? Thank you. <script> function myFunction() { var month ...

Compiling with GatsbyJs throws an abrupt token error with 'if' being an unexpected token

I am working on a code snippet in GatsbyJS where I am extracting data from a StaticQuery using GraphQL and then rendering a component. The challenge I am facing is to conditionally check if a specific sub-object exists within the data object, and if it doe ...

What is the best way to display a component with multiple pieces of content?

I have a tool that generates card components, extracting data from a const array and displaying it in a table format within a card UI component. I am looking to add an ADD button inside each card to open a modal with input fields. However, the issue is tha ...

What steps can you take to ensure compliance?

Whenever I input non-existing data into the search, I encounter an error message stating "Cannot read property 'general' of undefined." How can I create a condition to handle this error effectively? Despite my attempts to implement a condition, ...

In Vue JS, assign a boolean value to a variable based on the screen width

Currently, I am working on a responsive navigation menu. I have completed the mobile view, but now I face an issue for the desktop view. I want to hide the button that toggles the mobile navbar and only display the navbar. However, the navbar is linked to ...