Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-01-26
3
Medium Priority
?
132 Views
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
0
Comment
Question by:dentsplynz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 43

Assisted Solution

by:Rob
Rob earned 1000 total points
ID: 40571936
http://jsbin.com/nukawa/1/edit?html,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: obj.total});
}

console.dir(data);

Open in new window

0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 1000 total points
ID: 40572174
Hi,
and an alternative using the jQuery way utilizing the jQuery.map 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: element.total}});

Open in new window

Sample:
http://jsfiddle.net/EE_RainerJ/t21oasLa/

HTH
Rainer
0
 

Author Closing Comment

by:dentsplynz
ID: 40574525
Awesome! Thanks so much.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)
Suggested Courses

636 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