"Using a function parameter as a key in a JSON object

While I may not be a JSON expert, I am struggling to understand why this code is not functioning as expected and what steps I need to take to fix it. Despite my efforts to search for solutions, I suspect that my lack of familiarity with the correct terminology is hindering me. My goal is to call a function and, within that function, create a JSON object to store in an IndexedDB "table" like the following:

UpdateSettingsValue("initialized", true);

function UpdateSettingsValue(key, value){
    var tx = db.transaction(["settings"], "readwrite");
    var store = tx.objectStore("settings");
    var trans = {
        key:value
    }
    var request = store.add(trans);
}

Although this script successfully creates an entry, it saves it as {key: true}.

Therefore, my query is how can I alter it to save as {initialized: true} instead?

I appreciate your assistance!

Answer №1

To manually set the trans variable, avoid using literal syntax and initialize it directly:

function UpdateSettingsValue(key, value){
    var tx = db.transaction(["settings"], "readwrite");
    var store = tx.objectStore("settings");
    var trans = {};
    trans[key] = value;
    var request = store.add(trans);
}

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

Steps for successfully passing an object in a dynamically generated function invocation

In my jQuery script, I have a click event bound to thumbnail images with the class "thumbnail": $(".thumbnail").click(function(){ var obj = new Object(); obj.el = "imageEdit"; obj.id = $(this).attr("id").replace("img_",""); openDialogue(obj); }); ...

Having trouble getting req.files to work in a Node.js Express application?

Hello there, I'm facing an issue with accepting an uploaded file. Every time I call req.files, it comes out as undefined. I can't seem to figure out what I am doing wrong... Below is a snippet of my app.js file: var express = require('expr ...

Using arrow functions in React to streamline unit testing with Jest

Every time I encounter an error that checkIfBlank is undefined, I recognize that I need to export the function. However, I am unsure how to do so for an Arrow Function. This is the code snippet in register.js import React from "react"; import ax ...

What is the best way to access the form button inside a div element?

Here is the code snippet for my form: <form accept-charset="utf-8" action="https:example.com" method="get" name="test"> <div class="classy"><input type="button" class="buttonE ...

Accordion jQuery failing to display tabs

I have a question about using 2 jQuery accordions with tabs inside them. Everything seems to be working, but when I try to expand the second accordion, the two tabs inside don't render properly. I'm not sure what the issue is. Any help on this wo ...

What is the best way to resize an image within a canvas?

As I work on displaying an image using the cover simulation in canvas, I came across a helpful solution. Currently, the image changes based on screen resolution, but only updates after a page refresh. https://i.sstatic.net/qEL26.gif Is there a way to ac ...

Lazy loading Angular component without route - Form control name has no value accessor

Exploring lazyloading a component without routing. I have two components, each with its own formGroup. The parent component, named vehicleForm, includes a FormControl named vehicleDetail. The child component's formGroup includes fields for fuel and ...

Having trouble loading a page in ReactJS when redirecting from another page?

I am working on a React application that begins with a login page. I want the login button on the login page to redirect to the home page, and then immediately redirect to the notifications page when I reach the home page. The routing operations should occ ...

AJAX Post Request Using CodeIgniter

Greetings to everyone! Thank you in advance for your help. I am facing an issue while developing a website using codeigniter, bootstrap, and jquery. The problem arises when I try to load a new view. Normally, when I load a new view without a post request ...

Include elements to the (sub) category within a nested JSON list

Dealing with a nested JSON-array can be tricky, especially when trying to assign subitems to one of its sub-objects. For example: jsonData[0].menu1[0].menu2[0].menuItem5 = "ok"; This code successfully adds an item with the text 'ok', which is ...

concentrate on the input box

Once I click on the CPM, my goal is to automatically place the cursor in the centralized field of this page: . Despite my attempts to use different code snippets in the console, I have not been successful. I experimented with document.getElementById("se ...

Clicking on the FLOT graph will trigger an expansion of the visualization

Currently, I am utilizing the flot library for data plotting functionality. Flot utilizes the height and width properties of a div to create the plotted data, as shown below: <div id="graph" style="width: 300px; height: 300px;"></div> I am in ...

Dynamically Generate Nested Objects in JavaScript

I have an object that is currently empty and I am looking to dynamically create a nested object within it. const obj = {} obj["test1"]["test1.1"] = x //initialize to some variable However, I encountered this error message: Uncaught Typ ...

"What is the best method to transfer a JsonObject from an Android Activity to JavaScript

Can someone please explain how I can send a JsonObject from my Android Activity to a Webview? Specifically, I want to pass the object to a JavaScript page. Any guidance on this matter would be greatly appreciated. ...

Using JS and d3.js to eliminate or combine duplicate connections in a d3.js node diagram

Hey there! Hello, I am new to working with js/d3.js and tackling a small project. Here are some of my previous inquiries: D3.js: Dynamically create source and target based on identical JSON values JS / d3.js: Steps for highlighting adjacent links My cu ...

Retrieve the accurate file name and line number from the stack: Error object in a JavaScript React Typescript application

My React application with TypeScript is currently running on localhost. I have implemented a try...catch block in my code to handle errors thrown by child components. I am trying to display the source of the error (such as file name, method, line number, ...

Exploring the Concept of Sending Data via AJAX Request

Trying to comprehend the intricacies of the HTTP POST request transmitted through jQuery's .ajax() or .post() functions. I'm puzzled by the presence of a 'datatype' parameter for server-sent data. What exactly will be included in the r ...

Troubleshooting: CSS file not functional within nested routes of ExpressJS

I'm currently working on a notes app to enhance my skills in routing using express. There are two endpoints: "/notes" and "/notes/add". The "/notes" endpoint displays a page with a list of all the notes created so far. ...

Displaying adornments in a vertical arrangement within a TextField using Material UI

Is there a way to display adornments vertically in a Material UI Textfield? I've been trying but it always shows up horizontally. Snippet: <TextField variant="filled" fullWidth multiline rowsMax={7} onFocus={() => h ...

Is there a way to streamline routing in AngularJS similar to how .NET Data Annotation automates tasks?

Understanding the routing system in AngularJS is key, as shown below: .when('/96', { templateUrl: "96.html", animation: 'present' }) .when('/96/moharram', { template ...