JavaScript's Map function is returning an undefined object even though the object does exist

Something strange is happening to me. I have a Map() filled with objects, but when I try to get an object using the map.get() method, I always receive 'undefined'.

The attached image provides a clear explanation of the issue. Why is X turning out to be undefined?

https://i.sstatic.net/89JzI.png

Thank you in advance for your help.

Answer №1

It seems there is a mismatch in data types.

console.log( "17" === 17 ); // returns false

To correctly match the key, you must convert id to a number:

var id = Number($scope.eventId); // id = 17

// ...

var y = mmap.get(id);   // y = ["LOG_IN", "DISABLED"], ...

In a Map, keys are matched using strict equality (===), which requires the values to have matching types first.

The keys used in the Map seem to be numeric:

Map { ..., 17 => ["LOG_IN", "DISABLED"], ... }

However, the variable id is currently a string:

id = "17"

This information is based on the highlighted output in the debugger shown in orange.

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

Ways to access text content in an HtmlTableCellElement

I am currently working on a jQuery tic-tac-toe project and facing an issue with iterating through the board to save the values of each cell in an array. I have tried using .text() and .value(), but both returned undefined index.html: <html> < ...

"Modifying the form-control-lg class in Bootstrap and AngularJS affects input size exclusively, leaving the label unaffected

Currently, I am focusing on enhancing an AngularJS custom text input component: <div class="form-group has-feedback" ng-class="[$ctrl.sizeClass, {'has-error': $ctrl.isNotValid(), 'has-success': $ctrl.isValid()}]" > ...

Using Javascript to handle form submission and enforce required input fields

Has anyone encountered an issue when trying to submit form data using a div, HTML5, and JavaScript? I noticed that if I use a submit button with the required attribute in the inputs, the validation works but the button doesn't actually submit the info ...

Is it possible to display a border in Bootstrap 4 depending on the resolution?

Can a border be displayed on the right for large resolutions only, but not for others? For example: <div class="border-lg-right">content here</div> ...

What is the best way to handle a single promise from a shared listener?

I am working on implementing an event listener that will receive events from a server whenever a specific task is completed. I want to structure each task as a promise to create a more organized and clean workflow. How can I resolve each task promise by i ...

Oops! The page you're looking for at /api/tasks

Whenever I try to access: http://localhost:3000/api/tasks, I keep getting a "Cannot GET /api/tasks" error message. This is my server.js: var express = require('express'); var path = require('path'); var bodyParser = require('body ...

I am having an issue where my Express/Angular application is not redirecting properly after

Currently in my Express setup: var router = express.Router(); router.get('/auth/*', function (req, res, next) { next(); }) app.use(router); app.all('/*', function(req, res) { res.sendfile('index.html', { root: __dirname ...

What is the process of transferring data from one div to another div (table) using AngularJS?

In my quest to enhance my table with JSON data upon clicking the + icon, I am faced with two sections: Pick Stocks where stock names and prices (retrieved from data.json) need to be added to the table found in the Manage Portfolio section. First Section h ...

Converting lengthy timestamp for year extraction in TypeScript

I am facing a challenge with extracting the year from a date of birth value stored as a long in an object retrieved from the backend. I am using Angular 4 (TypeScript) for the frontend and I would like to convert this long value into a Date object in order ...

Setting up the Angular2 library with a defaultExtension for systemjs configuration

Can I automate the setup of an npm repository for Angular2 to handle the systemjs config with default extensions? I'm currently working on ng2-orm and wondering if it's possible to streamline this process during npm installation. Although my pa ...

How can I effectively monitor and track modifications to a document's properties in MongoDB?

I'm wondering how to effectively track the values of a document in MongoDB. This involves a MongoDB Database with a Node and Express backend. For example, let's say there is a document within the Patients collection: { "_id": "4k2lK49938d ...

Utilizing ng-repeat Loop Variable in ng-include Content within Angular Framework

I am looking to create two tables with identical record structures. One table will serve as the Main table, while the other will act as an Archive. Each record is represented by bootstrap divs with col-sm-x structure containing details such as {{rec.Name}} ...

Why is the Jquery console not displaying any values?

Hey everyone, I need some help with a small issue in my project. For some reason, the console.log() function in my project is not returning any values. <script> $('#search-box<?=$x;?>').blur(function() { var val = $("#search ...

Angular's ability to support multiple controllers

Help needed! I am new to Angular and struggling to apply multiple controllers to my table. These controllers belong to different modules, specifically from the npm install module called angular-table-resize. However, the table resize feature doesn't s ...

The registration link for Google on my website is experiencing an error, while it works fine on the Allauth page

I am using the allauth module to authenticate users with Google. In the template /accounts/login/ provided by allauth, there is a link "google" that directs to href="/accounts/google/login/?process=login". Clicking on this link will authenticate the user ...

What is the best way to establish a link between the index and the JSON object?

I am working with a JSON object that maintains a specific sequence within it. var sample={ "sample": [ { "example": [ { "sequence": 1, }, { "sequence":2 }, { "sequ ...

Is there a method to display a loading animation while the micro apps are being loaded in a single spa project?

Currently, I am working on a project using single spa and I need to implement a loader while my micro app is being loaded. Additionally, I also need the loader to be displayed when switching between these micro apps. Are there any methods to accomplish t ...

Guide on updating a MongoDB document upon clicking a button on an HTML page

I'm new to backend development and I've been working on creating a CRUD notes application without using React or EJS. My current issue is that I am unable to edit documents. The desired functionality is for the user to be directed to a page wher ...

Challenges arising from incorporating a nested for loop with table output in JavaScript

var data = { "categories":[ "Early Years (RA)", "Primary schools (RA)", "Secondary schools (RA)", "Special schls and alter prov (RA)", "Post-16 provision (RA)", "Other edu and community budget (RA)", "TOTAL EDUC ...

The button that is disabled can still be triggered using the ".click()" function

When programmatically triggering a disabled button with an "onclick" function, the function is still fired. See example below: <div> <input type="button" onclick="save()" id="saveButton" value="save" disabled="disabled" /> <input ...