Given any object with members that look like this:
target
{
x1 : 1
x2 : 2
x3 : 3
y1 : 6
y2 : 5
y3 : 4
}
I need to transform the object into something like this:
target
{
x : [1, 2, 3]
y : [6, 5, 4]
}
From the basic JavaScript I know, it seems like the following function would work:
function joinParameters(target, paramName) {
var count = 1;
var array = [];
var name = paramName.concat(count);
var value = target[name];
while (typeof value != undefined) {
array.push(value);
name = paramName.concat(count);
}
target[paramName] = array;
}
So, I could say joinParameters(target, “x”); The problem, however, is that
var value = target[name];
is always undefined. I followed the code in FireBug to make sure that the target object has the property that I’m trying to get, and it does. So, I’m not quite sure what’s wrong.
If jQuery has an elegant way of doing this, then I would prefer that solution.
Using underscore.js:
Working example:
http://jsfiddle.net/PLgN5/