Creating arrays in SQL by adding keys to loop through the data

I am looking for a way to loop through all of my SQL data that I have parsed into a JSON table and add a key to each item in an array.

Currently, I can loop through the data and concatenate it into a simple string like this:

var dbString ="";
for(i = 0; i < result.response.length; i++) 
{
    dbString += result.response[i].names;
}

Which results in something like:

var dbString = "James Michael Alfred....";

However, I would like to format it differently like so:

var dbString = {"James":1, "Michael":2, "Alfred":3, .....}

If anyone has any suggestions on how to achieve this, I would greatly appreciate it. Thank you!

Answer №1

Creating a list of names in the specific format you requested is quite an interesting task, but I managed to do it:

const database = {};
const nameList = response.results;
for(let j = 0; j < nameList.length; j++){
    database[nameList[j]] = j + 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

The browser is not displaying the results from Mongodb, yet they are appearing in the console

I am currently using the following code for my router: let mongoose = require('mongoose'); // connecting with our model let ByProduct = require('../models/Byproduct') router.get('/',(req,res,next)=>{ ByProduct.find().th ...

Separate terms & mix / scramble characters

I am attempting to rearrange words by shuffling the letters within each word. Instead of shuffling the letters within the first word with its own letters, it is currently shuffling the first word with the second word's letters. My goal is to split th ...

Is the alert still displayed during a frozen UI, even when it is within a synchronous ajax call?

Here is the code snippet for implementing global AJAX functionality. jQuery(function (){ $(document).ajaxStop(function() { alert("stop"); }); $(document).ajaxStart(function() { al ...

Ways to randomly rearrange div each time a new page is loaded

I wrote some code to shuffle my div elements when the page is reloaded, but I really want them to be shuffled every time a new visitor lands on the page. Since I am not an expert in jquery/javascript, I decided to reach out here for some guidance. Here&ap ...

Angular/Typescript ESLint rule for enforcing pure functions

Are there specific ESLint rules to ensure that only pure functions are written in TypeScript? I am looking to strictly write only pure functions in my code. Could someone please provide guidance on how to accomplish this? Appreciate any help in advance ...

Incorporate pug and sass into your React project for a more

Are pug/jade and sass compatible with react? I enjoy organizing my code by separating files, and the functionality that sass and pug provide is great for this purpose. Is there a way to integrate pug and sass into a react project? Pug example: doctype ...

Use JavaScript or jQuery to implement the position absolute styling

I am currently working on a website where the navigation is aligned to the right side. However, I am facing an issue where the last menu item dropdown extends beyond the page because it is absolutely positioned to the left of its parent element. I am act ...

What issues can be found in this code snippet for AngularJS in the browser code editor that is interacting with Node.js

Greetings, I am currently working on developing a free in-browser code editor for high school students at the basic level to share knowledge. After extensive research, I came across this link. Following the instructions provided, I made some setups and mod ...

Unable to operate a playlist application

I am currently facing an issue while trying to run a small application using an HTML kit. The application consists of two forms - one for text input and the other for a button. The goal is to save the text entered in the text input form into a list when th ...

jQuery draggable elements can be easily dropped onto droppable areas and sorted

I need help with arranging the words in the bottom tiles by sorting them from "Most Like Me" to "Least Like Me" droppable areas. Currently, I am able to drag and drop the words into different boxes, but it ends up stacking two draggable items on top of eac ...

What is the best way to retrieve the name of a Meteor package from within the

As I work on developing a package, I am looking for ways to dynamically utilize the package's name within the code. This is particularly important for logging purposes in my /log.js file. My main query is regarding how I can access the variable that ...

Responsive mode causing navbar to break

After creating my web portfolio, I encountered an issue when viewing it on my phone. The navbar collapses but is not properly aligned and there is extra space on the right side of the viewport in the hero section that I can't seem to fix. Portfolio L ...

Convert a JSON array into an array of Java objects

When calling the API, I receive a response in JSON array format that needs to be mapped into an array of Java objects. Below you will find the model class definition along with the API JSON array, Model class, and my attempt at mapping the JSON array into ...

Trouble with directive causes Angular Slider Touch functionality to fail

Currently, I am implementing a directive in my application https://github.com/prajwalkman/angular-slider Most of the functionality is working fine except for touch support when trying to drag the sliders on the screen. Touch support seems to work on the ...

Access previous value in Vuejs onchange event

In the code snippet below, how can I retrieve the previous value of the model that has been changed, specifically the age in vuejs? var app = new Vue({ el:"#table1", data:{ items:[{name:'long name One',age:21},{name:'long name Two&a ...

Generating an array of struct or object from JSON in Swift

Looking for advice on the best method to store a list of Vendors from a JSON file within a class in a way that is easily accessible and can be passed throughout the project. Considering using a struct for this implementation as it's something new for ...

Saving a value in a service using AngularJS

I am facing an issue with passing a variable into a service that needs to be accessed from multiple states within my application. I have created a service and attempted to pass a variable from a controller into the service in order to access it from anothe ...

The code runs smoothly on JSFiddle, but encounters issues when run on a local server

Looking for assistance with this issue... I have a link referenced by the code http://jsfiddle.net/LTYa2/14/. It works perfectly on jsfiddle, but when I try to implement it on localhost, it doesn't work. I have confirmed that all JavaScript and jQuer ...

Optimizing JSON Data Structure Efficiency: Best Practices for Converting JSON to Objects

Struggling with the design of my web application involving JSON, ASP.NET, TypeScript/JavaScript, and AngularJS. In Brief: Seeking best practices for transmitting data from server to client via JSON, utilizing the JSON string on the client side to generate ...

Implementing jQuery and JavaScript validation for email addresses and usernames

Are the online validations being used incorrectly or is there a serious issue with them? I came across an example of a site using jQuery validation, but when I entered "44" for a name and ##@yahoo.com for an email address, no warning appeared. I haven&apo ...