Traverse through a JSON object while dynamically changing the key name

Can we loop through a JSON object with keys that contain sequential numbers?

Below is the specific JSON object being referenced:

{
  key0: 'adbid1,23',
  key1: 'adbid2,21',
  key2: 'adbid3,191',
}

This is the code I am using:

for (var i = 0; i < objectLength; i++) {
  var submitray = query.key[i].split(","); //error
  var qid = submitray[0];
  var userAnswer = submitray[1];
}

Answer №1

Consider this alternative approach:

let data = {
  item1: 'abc,123',
  item2: 'def,456',
  item3: 'ghi,789'
}

function processData() {
  for(let key in data){
    let splitData = data[key].split(",");
    let value1 = splitData[0];
    let value2 = splitData[1];
    console.log(value2);
  }
}
processData(); // call the function

Link to code

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

Accessing data from nested arrays in PHPHere are ways to

Currently, I am making API calls. The response I receive is in the form of an associative array, allowing me to access values like this: $field = $response['nameOfKey']; However, some keys have values that are arrays themselves. Take a look at ...

The date format in AngularJS is not being displayed correctly according to the request

My goal is to retrieve the date in the format of dd/MM/yyyy and pass it along to my URI. However, I am encountering an issue where the date is not being passed in the requested format. Below is the snippet of my HTML code: <script type="text/ng-templat ...

Ensure there is no blank space between the bootstrap labels when using ng-repeat

When using ng-repeat in Angular.js to add labels, they appear without spacing. Check out this Plunker for a demonstration. However, if I add labels manually by copying the HTML code, they are displayed with whitespace. Is there a way to add white space b ...

Can someone explain to me the utilization of async await in conjunction with map in mongoose?

const fetchOwnerInfo = async _ => { console.log('Initiating search for book owners...') const ownerPromises = books.map(async (book) => { const ownerDetails = await User.find({_id: book.Owner}) ...

Close to completing the AngularJS filter using an array of strings

I'm currently working on developing a customized angular filter that will be based on an array of strings. For instance: $scope.idArray = ['1195986','1195987','1195988'] The data that I aim to filter is structured as fo ...

Experience the classic game of rock-paper-scissors brought to life through JavaScript and HTML,

I've been working on a rock, paper, scissors game in JavaScript and I'm struggling to get the wins and losses to register correctly. The game keeps resulting in a draw even though I've checked my code multiple times. It's frustrating be ...

Is the for loop in Node.js completed when making a MySQL call?

A certain function passes an array named res that contains user data in the following format: [ RowDataPacket { UserID: 26 }, RowDataPacker { UserID: 4 } ] The goal is to create a function that fetches each user's username based on their ID and stor ...

TypeScript and the Safety of Curried Functions

What is the safest way to type curried functions in typescript? Especially when working with the following example interface Prop { <T, K extends keyof T>(name: K, object: T): T[K]; <K>(name: K): <T>(object: T) => /* ?? */; ...

How to use jQuery to hide radio buttons that are not checked when clicking a submit

Looking to dynamically hide all unchecked radio buttons and their labels until a submit button is clicked, displaying only the checked radio button. <form method="post"> <input type="radio" name="radiobtn"> <label for="first">Fir ...

What steps can be taken to ensure that the scale() function is given the highest priority

Trying to develop a program that generates a canvas graph based on some calculated data. To adjust for larger numbers, I attempted to scale the graph using ctx.scale();. However, every time I do this, the canvas goes blank! I even tried scaling the canvas ...

Implementing specific CSS styles for images that exceed a certain size limit

Currently, I am facing a dilemma as I work on developing a basic iPhone website that serves as a port for my blog. The main issue I am encountering is the need to add a border around images above a specific size and center them in order for the blog to hav ...

Running QZ-Tray within a function on Express is not possible

I've been working on a web app to print on an LQ-310 Printer using express and QZ-Tray. Unfortunately, I'm facing issues with the printing process - it only prints when users hit the endpoint directly. If I try to encapsulate the QZ-Tray function ...

Obtain directive content in AngularJS prior to compilation

Is there a way to extract the HTML content of a directive prior to compilation? For instance, consider the following: <my-directive> <ul> <li ng-repeat="item in items">{{item.label}}</li> </ul> </my-directive> ...

Recording the screen with electron and utilizing the angularJS framework

I've been exploring the implementation of the electron recording method into my electron and angularJS desktop application. Since I am using angularJS to maintain services, controllers, and views, I have a button in one of my views (HTML file) that i ...

Pattern matching tool for identifying React components with an unlimited number of properties

Currently diving into MDX and experimenting with Regex to extract details on React components from Markdown files. The regex pattern I'm aiming for should: Detect all types of React components This includes identifying the opening tag <Component ...

When I try to install dependencies with Hardhat, the "Typechain" folder does not appear in the directory

After installing all the dependencies, I noticed that the "typechain" folder was missing in the typescript hardhat. How can I retrieve it? Try running npm init Then, do npm install --save-dev hardhat Next, run npx hardaht You should see an option to se ...

What level of scalability (Big-O) is involved in searching through indexed data in mongoDB?

I have a design question regarding indexing keywords. Can you confirm if I am properly indexing the key words as shown below? obj = { name: "Apollo", text: "Some text about Apollo moon landings", tags: [ "moon", "apollo", "spaceflight" ] } Am ...

Error: NullPointerExpection - Trying to execute 'void com.mobiledealer.dao.OrderDAO.insertOrUpdate(com.mobiledealer.model.Order)' on a reference to an object that does not exist

Hey there, I'm having an issue with the response from a REST API. I receive a JSON object, parse it successfully, but when I try to add it to Realm I get a Null Pointer Exception. You can find my project here: https://github.com/666Angelus666/MobileDe ...

send Javascript syntax errors and console.log output to a different location

I'm experimenting with ways to redirect output from the javascript console to my own custom functions. Here's what I've attempted: window.console.error = window.console.debug = window.console.log window.console.log = mylog; function mylo ...

PostMan gives me an error when I attempt to send an image file to NodeJS using multer (req.file is undefined)

After encountering issues with saving image files to the server using multer-s3, I attempted to use multer and s3Client instead. Unfortunately, I faced similar challenges as req.file or req.files from the multer-s3 middleware continued to return undefined. ...