Set containing a nested collection of maps

Recently delving into the world of JavaScript and trying to grasp the concept of maps.

This is the code snippet I'm currently working with:

const topmap = new Map();

const map1 = new Map();
const map2 = new Map();

const set1 = new Set();

map1.set('x','y');
map1.set('a','b');

set1.add(map1);
set1.add(map2);
topmap.set('ID',set1);


console.log(topmap.get('ID').size);
topmap.get('ID').forEach(x => {
  if(x.has('x')){x.delete('x')} )
  
  }
)
console.log(topmap.get('ID').size)

Can someone help explain why both lines are returning a size of 2 even after deleting one map in line 18?

Answer №1

When dealing with the code snippet topmap.get('ID').size, it is important to note that this represents two map elements within your set1. In order to make changes, be sure you are deleting individual elements from the map rather than the entire map itself.

Answer №2

There is a minor issue in your code snippet above where you have an extra ) at the end of the line if(x.has('x')){x.delete('x')} ).

To remove the entire Map (map1) from the Set (set1), you can simply use the set1.delete(map1) method. Here's the corrected version of the code:

const topmap = new Map();

const map1 = new Map();
const map2 = new Map();

const set1 = new Set();

map1.set('x', 'y');
map1.set('a', 'b');

set1.add(map1);
set1.add(map2);
topmap.set('ID', set1);

console.log(topmap.get('ID').size);

// Delete the Map from the Set
topmap.get('ID').forEach(x => {
  if (x.has('x')) {
    set1.delete(x);
  }
});

console.log(topmap.get('ID').size);  // It should now log 1

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

What's the best way to ensure uniform spacing between the list items in this slider?

When using appendTo, I've noticed that the spacing between items disappears. I've tried adjusting the padding-bottom and left properties with no success. Does anyone have a suggestion for how to fix this issue? I'm at a standstill. $(&a ...

Instructions on how to delete a specific tag from a table using its ID

I have a complex HTML table generated by a PHP loop with multiple rows. My goal is to eliminate all the a tags within the td tag where the ID of the td tag is equal to 1 or another specified value. <table> <tr> <td>ID: 1</ ...

Changing the string "2012-04-10T15:57:51.013" into a Date object using JavaScript

Is there a way to transform the format "2012-04-10T15:57:51.013" into a Date javascript object using either Jquery or plain Javascript? ...

Deleting files with a dedicated function (Dropzone.js)

I need to implement functionality that removes the uploaded file when a 'cancel' button is clicked. Here's how my HTML code looks: <div reqdropzone="reqDropzoneConfig"> <form id="requisitionupload" class="dropzone dz-clickable" ac ...

Transferring information from a webpage to a popup using executeScript: A guide

I've successfully retrieved data from a website, but I'm struggling to send it to popup.js. I attempted to use executeScript with a callback, but it's not working for me. popup.js function getData() { chrome.tabs.query({active: true, c ...

Using AngularJs to implement a $watch feature that enables two-way binding

I am in the process of designing a webpage that includes multiple range sliders that interact with each other based on their settings. Currently, I have a watch function set up that allows this interaction to occur one way. However, I am interested in havi ...

Prevent the page from refreshing when a value is entered

I currently have a table embedded within an HTML form that serves multiple purposes. The first column in the table displays data retrieved from a web server, while the second column allows for modifying the values before submitting them back to the server. ...

Generate a graphical representation with react-d3

In an attempt to create a histogram using react-d3-components, I have the following code: import React, { Component } from 'react'; import * as d3 from 'd3'; import * as ReactD3 from 'react-d3-components'; import propTypes fr ...

What is the best method for resizing individual SVG elements using jQuery or JavaScript?

A unique effect on my webpage involves an SVG comprised of scattered squares that enlarge when the user scrolls past a specific point. However, there is a problem with the scaling process which results in a horizontal scroll bar appearing. This occurs beca ...

Troubleshooting: Custom JQuery function not functioning as expected

I am currently facing an issue with the jQuery in my website while trying to implement a portfolio element. It seems to be related to the changePortfolio() function, but I am unsure of how to resolve it. $('.projects a[href^="#"]').on('clic ...

My data is not appearing with ng-repeat or ng-bind

One issue I am encountering is that my ng-repeat / ng-bind is not displaying the data within $scope.articles, even though I am getting the expected data in the console. To help identify the problem more easily, I have created a code snippet below: var A ...

Routing WebSocket connections with Node.js

Currently, I am in the process of developing a chat application for my company which will run on node js with websocket (ws). The app is designed to cater to various departments within the organization, each with its own set of users. My goal is to ensure ...

Validation of a string or number that is not performing as expected

I have been working with the yup library for JavaScript data validation, but I am encountering unexpected behavior. Despite thoroughly reviewing their documentation, I cannot pinpoint where I am misusing their API. When I run the unit test below, it fails ...

How can I incorporate multiple graphs into my AmCharts display?

I am new to using amcharts and have successfully implemented a code snippet to generate two graphs in a chart. The charts are loaded from an external data source specified in the code. var chart = AmCharts.makeChart("chartdiv", { "type": "serial", "d ...

What is the best way to execute a function based on its name, which is provided as a parameter

Is there a better way to call a function by its name, passed as a parameter in JavaScript? Here's my scenario: I have a switch case where I need to set the data for an API ajax call. Once the ajax call is complete, I want to call a specific print func ...

Guide on looping through an array of objects and displaying the key and value pairs using pug

I'm having trouble displaying errors from an incomplete form. In my array of objects, I want to iterate through and retrieve the errors, but it seems the list is currently empty. Upon debugging, it appears that the errors variable contains [ { e ...

Implementing jQuery form validation including checking for the strength of the password

My understanding of jQuery was quite basic until I began working on jQuery form validation with password strength check. I successfully completed the password strength check portion, but now I am unsure of how to enable the submit button once the condition ...

Utilizing JavaScript recursion to navigate through a JSON object and update specific key-value pairs on its nested children

Exploring a JSON object structure that follows a tree-like child-parent relationship. Each node in the structure has a unique ID. Attempting to iterate through the entire object using a recursive function, encountering challenges with handling the children ...

What are the steps to utilize vue.js for dynamically adjusting my sidebar based on a URL input?

Greetings to the amazing community of Vue.js enthusiasts, I am a novice looking to develop a single-page web application using vue.js. This application will consist of a fixed header and dynamic body content that changes based on user interactions. Here&a ...

Error: The object 'require' is not recognized in Vue Component

I am facing an issue while using screenfull.js (import screenfull from "screenfull") in a Vue component. Can anyone help me with this problem? Here is the error information. Version: Vue: 2.6.14 @vue/cli-service: 5.0.4 Babel-loader: 8.2.5 Vue-loader: ...