For any object with elements that look like this:
target
{
x1 : 1
x2 : 2
x3 : 3
y1 : 6
y2 : 5
y3 : 4
}
I need to convert the object to something like this:
target
{
x : [1, 2, 3]
y : [6, 5, 4]
}
From the basic JavaScript that I know, it seems that the following function will 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 can say joinParameters (target, "x"); The problem, however, is that
var value = target[name];
always undefined. I monitored the code in FireBug to make sure that the target has a property that I am trying to get, and it is. So I'm not quite sure what happened.
If jQuery has an elegant way to do this, I would prefer this solution.
source
share