Add JSON elements to an array

Looking for help!

{"Task": [Hours per Day],"Work": [11],"Eat": [6],"Commute": [4],"Sleep": [3]}

Need to add these items to a jQuery array.

I've attempted using JSON.parse without success.

Typically, I can push parameters as follows:

MyArr.push(['Task','Hours per Day']);
MyArr.push(['Work','11']);
MyArr.push(['Eat','6']);

Is there a way to achieve the same with the JSON string?

Answer №1

Is it possible to simply convert the JSON data into an object and iterate through its properties using a loop?

let jsonData = '{"Task": ["Hours per Day"],"Work": [11],"Eat": [6],"Commute": [4],"Sleep": [3]}'
let objData = JSON.parse(jsonData);
let arr = [];
for (let prop in objData) {
    arr.push([prop, objData[prop][0]]);
}
console.log(arr);

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

Add an item to an array that contains objects within an array of other objects

How can I properly push the values "label" and "link" into an object within "data" based on the id match with the "parent" value of another object? The goal is to insert these values into the "children" property of the corresponding target object, but it d ...

Can the tooltip on c3 charts be modified dynamically?

Creating a c3 chart involves defining various properties, including a tooltip. Here is an example: generateData = () => { const x = randomNR(0, 100); const y = randomNR(0, 100); const together = x + y; return { data: { columns: [ ...

tips for transferring API JSON response parameters to material ui table

I'm currently working on creating a table with the material UI. My goal is to populate the rows of the table based on the data received from an API response. The challenge I'm facing is that the API response includes an array of objects with mult ...

The JSONP request connected to the user input field and button will only trigger a single time

Hello all, I'm new to this and have been searching the internet high and low to see if anyone has encountered a similar issue before, but haven't had any luck. I've been attempting to set up a JSONP request to Wikipedia that is connected to ...

How should the nonce be properly set in the csp policy?

I've been attempting to incorporate a nonce into the csp policy but it's not functioning as anticipated. Here's the code snippet I'm currently using for testing purposes: server.js express.use(function(req, res, next) { res.set( ...

Is there a way for me to retrieve SCSS color variables within the javascript of a Vue template?

I have a unique challenge in my application involving a component called Notification. To bring notifications to other components, I utilize the mixin method toast('message to display', 'color-variable'). My goal is to set the backgroun ...

Tips for verifying the contents of a textbox with the help of a Bootstrap

I am still learning javascript and I want to make a banner appear if the textbox is empty, but it's not working. How can I use bootstrap banners to create an alert? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&g ...

Issues with retrieving the subsequent anchor element using Jquery Slider

(Apologies for the lengthy post. I like to provide detailed explanations.) Just starting out with Jquery, trying my hand at creating a custom image slider from scratch. Got the slider working with fade effects using this code: Javascript: $(document).re ...

Tips for minimizing object instantiation within a Java for loop

In my Java program, I am generating a JSON file with the following code: JSONObject json = new JSONObject(); JSONArray vertex = new JSONArray(); for (int i = 0; i < num; i++) { JSONObject usr1 = new JSONObject(); JSONObject usr2 = new JSONObjec ...

Is it possible to prompt npm to install a sub-dependency from a GitHub pull request?

Currently, I'm in the process of setting up geofirestore, which has a dependency on geofirestore-core. However, there is an existing bug in geofirestore-core that has been addressed in a pull request. How can I make sure that my geofirestore installa ...

Why is my MySQL query not returning the most recent results when using setInterval()?

I am currently facing an issue with the setInterval function within the $(document).ready(function(){} My approach involves using setInterval to call a PHP script that executes MySQL queries to check the status of 4 switches and then updating the screen w ...

Form that adjusts input fields according to the selected input value

I am in need of creating a dynamic web form where users can select a category and based on their selection, new input fields will appear. I believe this involves using JavaScript, but my searches on GitHub and Stack Overflow have not yielded a suitable sol ...

Steps to extract an array from a data frame

Is there a way to extract an array from a column in a data frame based on a certain condition? For instance: data = data.frame(pn=c('a','b','c','d','e','f'), price=c(1,2,3,4,5,6)) If we have a l ...

What is the best way to ensure that all website users receive the same update at the same time using AngularJS and Firebase?

Imagine a scenario where I and my 3 friends are accessing the same website from different computers simultaneously. Each of us has a profile stored in an array like this: $scope.profilesRanking = [ {name:"Bob", score: 3000}, {name:"John", score: 2 ...

Decoding a JWT Base64 string into JSON format

While attempting to convert a base64 encoded JWT header into a string, I encountered an unexpected output: {"alg":"RS256","typ":"JWT","kid":"1234" The output seems to be missing the closing bracket. However, when I decode the same base64 string usi ...

Troubleshooting Java REST service integration in AngularJS for UPDATE and DELETE operations

After successfully implementing a REST service with Java and testing all HTTP methods using Postman, I decided to explore AngularJS. Upon integrating it to consume the REST service, I encountered issues specifically with the Delete and Put methods not func ...

Generate new variables based on the data received from an ajax call

Suppose there is a .txt file containing some integers separated by spaces, like '22 1 3 49'. I would like to use Ajax to read the file as an array or list, and then save each integer as a JavaScript variable. Currently, this code reads all cont ...

A guide to effortlessly converting Any[] to CustomType for seamless IntelliSense access to Properties within Angular/Typescript

Our Angular service interacts with the backend to retrieve data and display it on the UI. export interface UserDataResponse { id: number; userName: string; } AngularService_Method1() { return this.http.get<UserDataResponse[]>(this.appUrl + "/Ap ...

There are two identical instances of the function and class named "Ajax" within the prototype

Within the current project I am involved in, we have incorporated and utilized a function called Ajax repeatedly throughout various sections. function Ajax (recvType, waitId) I am considering implementing the "prototype framework" for the Ajax class. Sh ...

Turning off strict mode in the bundling process of React with webpack can be achieved by following

I'm facing an issue with my application. It works perfectly in all browsers except for IE, where it throws the following error: 0x800a0416 - JavaScript runtime error: Multiple definitions of a property not allowed in strict mode In my webpack.config ...