Create a new parent element for every element in the current list

I have a JSON object with different cryptocurrency values:

{
  "BTC": "0",
  "XCP": "0",
  "NOBL": "0",
  "USDE": "0",
  "SOC": "0",
  "KDC": "0",
  "DOGE": "0.00000001"
}

What I want is to convert it into an array of objects like this:

[
 { "Coin": "BTC",
    "Value": "0"
 },
 { "Coin": "XCP",
    "Value": "0"
 },
 { "Coin": "NOBL",
    "Value": "0"
 },
 { "Coin": "USDE",
    "Value": "0"
 },
 { "Coin": "SOC",
    "Value": "0"
 },
 { "Coin": "KDC",
    "Value": "0"
 },
 { "Coin": "DOGE",
    "Value": "0.00000001"
 }
]

I am not sure how to achieve this, maybe using a foreach key function, but any guidance would be appreciated. Thank you.

Answer №1

To iterate through the original collection, you can utilize a "for (var x in y)" loop:

var originalData = {
  "BTC": "0",
  "XCP": "0",
  "NOBL": "0",
  "USDE": "0",
  "SOC": "0",
  "KDC": "0",
  "DOGE": "0.00000001"
};

var newData = [];

for (var currency in originalData) {
    newData.push({
        Currency: currency,
        Amount: originalData[currency]
    });
}

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 issue with Infinite Scrolling/Pagination feature in backbone.js persists

My goal is to implement an infinite scroll feature similar to Twitter using the Pagination plugin for backbone.js. Clicking the button/link #pagination a should load the next page of results from the backend and append it to the current view PhotoListView. ...

remove all clicks from jQuery queue

I am facing an issue with a click event that triggers other click events, resulting in the addition of elements to the DOM. EDIT: However, upon clicking it multiple times, additional elements are added each time. Checking the jQuery queue confirms that an ...

The content is displayed below the navigation bar rather than beside it

I'm having an issue with the layout of my webpage. The new content I add appears below the navbar on the left side of the page instead of beside it. Can someone help me troubleshoot this problem? Below, you'll find the relevant HTML and CSS code ...

AngularJS | Dependency Injection appears to be silent and non-responsive

I've hit a roadblock in finding solutions to my query. In essence, I'm aiming to achieve dependency injection by linking my directive from the 'directives.js' file to be accessible in my controller within the 'controllers.js' ...

What is the recommended way to retrieve the Nuxt `$config` within Vuex state? Can it only be accessed through store action methods?

After using the dotenv library for my .env file, I had to change the runtimeConfig in order to secure my project's secret key. In my most recent project, I utilized nuxt version "^2.14" in SPA mode, so I only utilized "publicRuntimeConfig" in my nuxt ...

Guide to adding a default selection in a drop-down menu and making it unselectable with the help of jQuery and

I have three dropdown menus. After selecting a value in the first dropdown, I am using jQuery and AJAX to populate the second dropdown. My goal is to have a default item in the second dropdown like Select a value. Below is my JSP code: <!DOCTYPE HTML& ...

Having difficulty customizing the layout with JavaScript

I want to demonstrate the ability to send a message without hardcoding all messages in my frontend. I utilized existing code from this link shared by another developer. I have included the id="msg" in the input field and id="chatpanel" with class chat-pan ...

Displaying handpicked phrases [Javascript]

When you click your mouse on a sentence, the words inside are highlighted. This feature works flawlessly. However, trying to display the selected words using the button doesn't seem to be functioning as intended. JSFiddle words = []; var sentence ...

Is there a way to choose all elements in the Bootstrap pagination code using JavaScript?

Recently, I've been working on a website with Bootstrap, and I encountered an issue with the overflow scroll bar that I had to hide using CSS. To navigate the pagination with the mouse wheel, I've been experimenting with JavaScript. I found that ...

The connection to the Alexa Skill Activation API is failing to fetch

I'm currently working on integrating account linking into an Alexa skill. I've successfully obtained the access token, but I'm encountering an error when attempting to call the Alexa Skill Activation API. Below is my code snippet and the ass ...

Tips on positioning content beneath a fixed header or navigation bar when viewed in a web browser

Hi, I'm having an issue with creating a fixed header using HTML and CSS. When I set my header to be in a fixed position, it covers up the content below it. I want the content to be positioned under the header when the page is loaded. Additionally, I&a ...

Utilizing the setNetWorkConditions function in webdriverjs for Chrome

Is there a way to properly utilize the webdriverjs setNetworkConditions() method as outlined in the official documentation? This is what my code looks like: const chromeCapabilities = webdriver.Capabilities.chrome() const chromeOptions = { ...

Issues Encountered During Form Data Transmission via XHR

I require the ability to transfer files from one cloud service to another using Azure Functions running Node. In order to do this, I have installed the necessary packages (axios, form-data, xmlhttprequest) and am coding in VSCode. However, when dealing wi ...

Issues arise when using multiple where clauses with ReactJS Firebase

I am currently working on an application that requires fetching users based on their preferred preferences, but I am experiencing issues with the sorting feature. export const fetchUsers = (minAge, maxAge, prefer, gender) => db.collection('profi ...

Modifying the array structure will deselect all individual <Input> elements that have been iterated

Hoping to create a system for adding/removing sub-items with buttons that increment/decrement slots in an array, where input fields are automatically added and removed: <div *ngFor="let item of itemsInNewOrder; let i = index"> <input [(ngModel) ...

Can you show me how to design a website with an embedded browsing window and a customized right-click menu?

I am looking to develop a website that includes a window or frame where users can browse other websites. Additionally, I am interested in implementing a custom right-click menu within this browsing window. While I know that using iframes can help with th ...

Retrieving information from Prismic API using React Hooks

I'm having trouble querying data from the Prismic headless CMS API using React Hooks. Even though I know the data is being passed down correctly, the prismic API is returning null when I try to access it with React Hooks. Here is my current component ...

Using HTML and JavaScript to shift window positions within the AIR framework

Currently, I am in the process of creating an AIR framework application using HTML and JavaScript/jQuery within Aptana 2.0. To enhance user experience, I have eliminated the toolbox (<systemChrome>none</systemChrome>), but now I am looking to p ...

Swapping out a character on a webpage using jQuery

Can someone help me remove the colon from this code snippet on my webpage? <h1><b>Headline:</b> something</h1> I'm looking for a simple solution using jQuery, as I am a beginner in JavaScript. Please include the complete code ...

What is the best way to streamline these two JavaScript lines and eliminate redundancy?

In order to address an issue, I implemented the following two lines of code: MyString = div.getAttribute('data-type') || div.getAttribute('data-id'); MyString = MyString.replace('RemoveThis', ''); Although the code ...