Reputation: 589
I need to restructure my data so that it fits in with another piece of code. I need to convert my data (which is a name,value pair) into the following structure so that that it is processed correctly. Using JQuery how would I do so?
{items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
{value: "54", name: "Kelly Slater"},
{value: "55", name: "Rudy Hamilton"},
{value: "79", name: "Michael Jordan"}
]};
Right now I've got a $.each
loop which is feeding each name,value pair in but I don't know how I'd get that exact structure.
Upvotes: 1
Views: 310
Reputation: 239301
Why use jQuery? Do it with javascript.
The following assumes your source is an object named values
with properties, as opposed to an array with numeric indices:
var output = { items: [] };
for (key in values) {
if (values.hasOwnProperty(key)) {
output.items.push({ value : values[key], name: key });
}
}
Upvotes: 7