0

I have a web service returns this data

[
  {
    "Campaign": "default",
    "TotalInBound": 216.0
  },
  {
    "Campaign": "direct",
    "TotalInBound": 10.0
  },
  {
    "Campaign": "Sales",
    "TotalInBound": 151.0
  },
  {
    "Campaign": "Support",
    "TotalInBound": 2.0
  }
]

I am using a jquery library which its input is like this:

data = [
        { label: "Product 1", data: Math.floor (Math.random() * 100 + 250) }, 
        { label: "Product 2", data: Math.floor (Math.random() * 100 + 350) }, 
        { label: "Product 3", data: Math.floor (Math.random() * 100 + 650) }, 
        { label: "Product 4", data: Math.floor (Math.random() * 100 + 50) },
        { label: "Product 5", data: Math.floor (Math.random() * 100 + 250) }
    ];

I want to change my data to that form of data . for example, the final result will be like this:

data = [
{label:"default", data:216.0},
{label:"direct", data:10.0 },
...
...
];

could you help please. i tried to use loops many times, but nothing works.

2
  • 1
    Please share with us your attempts so that we can identify the problem and suggest some solutions. Commented Apr 10, 2014 at 13:39
  • @Lix actually I tried to make a loop on the first json. then I didn't know how to contact the loop with the new json. Commented Apr 10, 2014 at 13:40

1 Answer 1

2

Ehh, like so:

var newData = [];
for (var i = 0; i < currentData.length; i++) {
    var obj = {}
    obj.label = currentData[i].Campaign;
    obj.data = currentData[i].TotalInBound;
    newData.push(obj);
}

newData will now be an array in the correct format.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 I will accpet your answer after 8 minutes. thanks a lot . you great

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.