I have an object being returned by a legacy server and I want to change the structure on the client-side via JavaScript, jQuery, or even Underscore.js.
Below is what my original object looks like:
[
{
"Id":{
"LValue":1,
"Value":1
},
"Date":{
"LValue":"2013-10-17T00:00:00",
"Value":"2013-10-24T00:00:00"
},
"User":{
"LValue":508,
"Value":507
},
"Comments":{
"LValue":"This a test load",
"Value":"This a test"
},
"Name":"John Doe",
"IsDeleted":false
}
]
On the client-side though, I would like to flatten it to get the "Values" and stuff the "LValues" into a separate property so I don't loose them if I need it later:
[
{
"Id":1,
"Date":"2013-10-24T00:00:00",
"User":507,
"Comments":"This a test",
"Name":"John Doe",
"IsDeleted":false,
"LValues": {
"Id":1,
"Date":"2013-10-17T00:00:00",
"User":508,
"Comments":"This a test load"
}
}
]
this would make working with the object so much easier and any help would be deeply appreciated!
var oList = [
{
"Id":{
"LValue":1,
"Value":1
},
"Date":{
"LValue":"2013-10-17T00:00:00",
"Value":"2013-10-24T00:00:00"
},
"User":{
"LValue":508,
"Value":507
},
"Comments":{
"LValue":"This a test load",
"Value":"This a test"
},
"Name":"John Doe",
"IsDeleted":false
}
];
var newFormat = _(oList).map(function(o) {
var flattened = { LValues: {} };
_(o).each(function(val, propName) {
flattened[propName] = val.Value ? val.Value : val;
if(val.LValue) {
flattened.LValues[propName] = val.LValue;
}
});
return flattened;
}