Ways to transform an array into a formatted JSON object

Hey there! I need help converting this data using JavaScript

{Mango: 44, Banana: 85, Pineapple: 78}

into

["Mango", "44"], ["Banana", "85"], ["Pineapple", "78"]

Answer №1

It appears that the correct format is

{Apple: 134, Orange: 223, Peaches: 143};

as arrays do not use : for separation.

You can try this solution:

var obj = {Apple: 134, Orange: 223, Peaches: 143};
var output = Object.keys(obj).map( function(key){
  return [ key, obj[key] ]
});

Answer №2

Give this a shot:

let data = JSON.stringify(object);

Functioning well on the latest browsers.

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

Passing an array or set from jQuery to PHP with the help of $.ajax, with PHP incrementing a

I am attempting to pass an array to a PHP script: usedAnswers = [1,2]; // function for displaying a question displayQuestion = () => { $.ajax({ url: "backend/retriveData.php", data: {usedAnswers:usedAnswers} , ...

What is the method to successfully validate a unique key with Mongoose only if the other key is distinct?

Imagine a scenario where we define this model : const userSchema = new mongoose.Schema({ username: { type: String, unique: true, uniqueCaseInsensitive: true, required: true, }, organisation: { type: Schema.Types.ObjectId, ref: ...

Is JavaScript able to interpret a returned string as a script, or does it simply recognize it as a regular

Is it feasible to execute a function that generates a line of output which can be interpreted by javascript as a variable rather than a string? I've fetched JSON data and my goal is to extract object data and dynamically generate variables from it on ...

JavaScript inheritance through prototypes and the properties of objects

I am currently exploring the concept of prototyped inheritance in JavaScript for a function. This process is well documented in Wikipedia's javascript article. It functions smoothly when dealing with simple JavaScript types: function Person() { t ...

What is the most effective way to obtain a relative path in JavaScript?

I recently coded this Javascript snippet in a .js file for my ASP.net web project. It's used to fetch device types: function getDeviceTypes() { var deviceTypes; $.ajax({ async: false, type: "POST", url: "Controls/Model ...

Organizing and sifting through JSON files housed in Redis

Considering a switch from MongoDB to Redis for a highly updated JSON data store, with around 50,000 updates per second and potential for up to a million records. Currently, clients are using MongoDB's query language for result sorting and filtering, ...

Error encountered when attempting to update AWS CloudFront with exceeded rate allowance

As I attempt to delete a CloudFront distribution using the AWS SDK for JavaScript, I encountered an issue when trying to update it. Each time I send the request, I receive an internal server error stating "Rate exceeded". I have experimented with various v ...

Callback is triggered after ng-if removes the directive from the scope

A scenario on my page involves an angular directive nested within ng-if. I've developed a service that features a function, let's name it functionX, capable of accepting a callback as an argument. Whenever the ng-if condition becomes true, the ...

When attempting to JSON Deserialize with .Net, it appears to only parse a single object from my JSON

I have encountered an issue while trying to deserialize my json data from a file. The problem is that my jsontextreader seems to only read one object from the file instead of reading until the end. Surprisingly, I am not receiving any errors during the bui ...

Extract information from JSON-formatted string

Currently, I am retrieving JSON data in Arduino using HTTP methods and storing it within a String object. The data structure is as follows: { "item": { "Identity": { "Id": "327681", "ItemId": "64006A962B71A1E7B3A0428637DA997C.327681", ...

Retrieving the data input from a datalist and using v-model will automatically reset the datalist

Looking for a way to preserve options in a datalist after selecting one using v-model in Vue. Currently, the selected option clears all other options due to two-way binding. Is there a method to only capture the selected value without removing the rest of ...

What are some ways to implement offline caching in iOS app development?

I'm currently developing an iOS app for reading articles. The app retrieves article content through JSON data and displays it in a UITableView that includes images and text. To improve the performance of the app, I am looking to implement offline ca ...

Tips for effectively managing Angular JS dependencies and promoting modularity

As I embark on a new project from the ground up, my main focus is creating a responsive website optimized for mobile devices. In order to achieve this goal, I am seeking information about Angular: How does Angular manage its resources and dependencie ...

Adjusting the width of the container <div> will automatically resize the inner <div> to match

I have a main container element. .wrapper{ border:1px solid blue; position:relative; top:20%; left:30%; height: 300px; width: 350px; } When I adjust the width of the parent container, the child box does not reposition itself accor ...

Utilizing MEAN.js for uploading images

Following a tutorial on the MEAN stack, I am looking to implement an image upload feature using MongoDB on a server. Resources: Angular directive for file uploads create-spot.client.view.html <div data-ng-controller="SpotsCreateController"> &l ...

Discover the method for displaying a user's "last seen at" timestamp by utilizing the seconds provided by the server

I'm looking to implement a feature that displays when a user was last seen online, similar to how WhatsApp does it. I am using XMPP and Angular for this project. After making an XMPP request, I received the user's last seen time in seconds. Now, ...

Tips for activating a deactivated input field in ReactJS

I am trying to create a feature where my textfields are automatically disabled, but can be enabled for editing by clicking an 'Edit' button. Clicking the button again should disable the textfields and save any edits made. I have experimented with ...

Error: The function $(...).ekkoLightbox is not defined

Recently, I attempted to integrate ekkolightbox into my WordPress website. However, each time I selected an image, I encountered the following error message: Uncaught TypeError: $(...).ekkoLightbox is not a function Initially, I suspected that the issue ...

Converting Jackson output to JSON in Angular

The server I'm currently working with made a change to the REST format, transitioning from plain JSON: { "removedVertices": [ { "id": "1", "info": { "host": "myhos ...

Issue with Cordova ListPicker plugin while using the Ripple emulator

Currently, I am utilizing Cordova within Visual Studio and have successfully integrated the ListView plugin from this source. The addition of the plugin was done through the config.xml editor screen. While trying to execute window.plugins.listpicker.showP ...