Utilize strings as object keys in JavaScript

Let's say I have the following variables:

var myKey = "This_is_my_key"
var myObj = {"This_is_my_key" : true}

What is the proper way to access myObj using the key myKey?

Answer №1

Have you considered attempting the following approach?

let key = "My_unique_key";
let value = false;

object[key] = value;

This is equivalent to

object.My_unique_key = false;

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 is the process for updating the combination selector for each product within a specific category in PrestaShop 1.7?

We have a range of products in a specific category, each offering multiple pack sizes with varying prices (e.g. 1, 3, 5, 10, 25, 50, 100). EDIT: The homepage features these products displayed using an owl-carousel within a div element: https://i.sstatic.n ...

AngularJS: Functions may return false due to the asynchronous nature of services

Template Overview: <div data-ng-if="budgetSummaryExists()"> <span>You currently have no budget.</span> <button type="button" class="btn btn-danger" data-ng-click="setRoute('budgets')">Creat ...

Setting a variable value on ng-click in AngularJS - is it possible?

Currently, I am working with a modal that relies on ng-show="prefs" to control its visibility. I am aiming to have the close button within the modal update $scope.prefs to false. Additionally, I want an anchor tag to change the value to true. Yet, most of ...

Unable to get jQuery date picker to function on dynamically generated input fields through ajax

I'm facing an issue with my jQuery date picker not working on an input field generated via Ajax from the server-side, but it works fine when the field is directly added to the page. Here's a simplified version of what I'm dealing with: The ...

Issues with React Native imports not functioning properly following recent upgrade

Hey there, I’ve been tasked with updating an old React-Native iOS project from version 0.25.1 to 0.48.0. However, I’m encountering several compiler issues and struggling to navigate through the code updates. The project includes an index.ios.js file s ...

Executing polymorphism in Javascript without the use of OOP classes

In JavaScript or other object-oriented programming languages, polymorphism is achieved by creating different types. For instance: class Field {...} class DropdownField extends Field { getValue() { //implementation .... } } Imagine a library f ...

Steps for embedding a custom function in a switch statement

I am attempting to run a switch statement based on the argument provided to the function below. I have been trying to call different functions depending on the argument passed. However, I encountered an Uncaught ReferenceError in the beginning of the .js f ...

Unraveling nested JSON: A guide to deserializing JSON data in Jackson

{ vendors: [ { vendor: { id: 367, name: "Kuhn-Pollich", company_id: 1, } }, { vendor: { id: 374, name: "Sawayn-Hermann", company_id: 1, } }] } I am struggling to deser ...

Connecting HTML POST to MongoDB using NodeJS

I am in the process of developing a blog application using Node, Express, and MongoDB. My goal is to create an HTML form that will allow users to submit new blog posts directly to the database. While I have successfully implemented GET and POST requests us ...

Update the text for the filter search placeholder in the Ant Table component

Is there a way to alter the default placeholder text in the Ant Table? I've set up a functioning example in documentation but couldn't find any prop for customization besides the customized filter dropdown, which I didn't want to implement. ...

You cannot make a hook call within the body of a function component, unless you are using useNavigate and useEffect in conjunction with axios interceptors

Currently, I am delving into React and attempting to include a Bearer token for private API calls. My approach involves writing a private axios function to intercept requests and responses. Subsequently, I invoke it in my API.js file to fetch data from the ...

Is Next.js Dynamic Routing Failing You?

Recently, I attempted to implement Dynamic routing for a recipe app but encountered an issue where the page was not found. This problem has left me puzzled as I am fairly inexperienced with TypeScript, although not with React. // pages/recipes/[recipeId].t ...

Guide for implementing onclick event for info boxes in push pins on Bing Maps using ReactJS

I have successfully placed all the coordinates on the map using pushpins, but now I want to create an event that shows an infobox when a pushpin is clicked. Could someone provide an example of how to hide infoboxes and only show them when a pushpin is cli ...

Make sure to wait for the scrollTo function to finish before executing any other commands

I have a sleek scrolling directive in my AngularJS app that automatically scrolls to the bottom of the page. I want this command to execute only after the scrolling has completed. Currently, I trigger the scroll function and then use $('#comment-input ...

How can I search for a particular string in JavaScript?

I have a unique custom div that has been modified to function as an input element. Inside this custom div input element, there is a <p> tag with a default placeholder text. My goal is to verify whether the content of this div is empty or contains new ...

Enhance your JavaScript code by replacing Promise syntax with Async syntax and utilizing map() instead of a traditional For

I have a code snippet here that is functioning properly. However, I am interested in converting the Promise code in the middle of the function to Async code and replacing the for loop with map(). Can someone guide me on how to achieve this transformation ...

Resolving "Module not found: Error: Can't resolve 'url'" issue in Angular 14 while invoking web3 smart contract function

How do I resolve the web3-related errors in my Angular 14 dapp? I attempted to fix the issue by running npm i crypto, npm i http, and more. Every time I try to call a function from a smart contract with code like this.manager = await report.methods.mana ...

The data in the Android database is failing to update

Values are retrieved from a JSON server URL using a JSON parser and stored in the local device database to display on a mobile screen. However, after updating the server and running the app again, the values are not being updated in the local database. An ...

Setting a cookie in Laravel 5 through an AJAX request

After the user clicks on "add to cart," a new cart is created and the product is added to the user's cookie. However, I am facing an issue with setting the cookie on an ajax response. Here's what I have tried: // Setting the values for the view ...

The Vue.js application is experiencing issues with the v-for functionality

I am working on a Vue.js application where I am fetching a list using ajax: $.ajax({ method: 'POST', dataType: 'json', url: this.base_info.url + 'getavailability?token=' + this.token, data: this.search_inf ...