How do I convert data from one format to another format using JavaScript/jQuery ?

Posted on 2015-01-26
Last Modified: 2015-01-27
I have a json_encoded data assigned to a js variable

var dataToConvert = [
{"department" : "IS & IT", "total" : "24239.50" },
{"department" : "Finance & Legal", "total" : "250.00"}

Open in new window

How do I convert the above data to the following format ?

var data = [
{value : 24239.50, label : "IS & IT"},
{value : 250.00, label : "Finance & Legal"}

Open in new window

This is to generate data dynamically for CharJS Pie Chart
Question by:dentsplynz
LVL 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE earned 250 total points
ID: 40571936,js,console,output

var dataToConvert = [
  {"department" : "IS & IT", "total" : "24239.50" },
  {"department" : "Finance & Legal", "total" : "250.00"}

var data = [];

for (var i in dataToConvert) {
  var obj = dataToConvert[i];
  data.push({label: obj.department, value:});


Open in new window

LVL 44

Accepted Solution

Rainer Jeschor earned 250 total points
ID: 40572174
and an alternative using the jQuery way utilizing the function:
var dataToConvert = [
{"department" : "IS & IT", "total" : "24239.50" },
{"department" : "Finance & Legal", "total" : "250.00"}
var data = $.map(dataToConvert,function(element,index) {
    return {label: element.department, value:}});

Open in new window



Author Closing Comment

ID: 40574525
Awesome! Thanks so much.

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now