I have a large object of arrays. I need to break down this object into an array of objects. I believe that underscore.js is good tool for this type of object transformation, but I’ve never used that library before. For this example I need to convert the key of each property to a ‘name’ variable in the output array, and than push elements of each property of each object into array's. That’s difficult to explain so below I have a before and after array to help visualize what I’m trying to accomplish. Can I accomplish this task with underscore.js easier than pure javascript ? I’ve attempted this with for loops and if else statements but it got messy quickly, so any help is greatly appreciated.
Before:
var obj = {
"AH5T5TAFXX-001":
["AH5T5TAFXX-001",
{
Bin_reads:2436307,
IC_lot:1,
LabChip_size_bp:410,
LibType:"RNA",
Tot_reads:7386376,
bioSple:193,
internal_controls:5
}, {
Bin_reads:2906003,
IC_lot:1,
LabChip_size_bp:395,
LibType:"RNA",
Tot_reads:6680167,
bioSple:198,
internal_controls:5
}],
"AH5NVVAFXX-002":
["AH5NVVAFXX-002",
{
Bin_reads:2436307,
IC_lot:1,
LabChip_size_bp:410,
LibType:"RNA",
Tot_reads:7386376,
bioSple:193,
internal_controls:5
},
{
Bin_reads:2436307,
IC_lot:1,
LabChip_size_bp:410,
LibType:"RNA",
Tot_reads:6680167,
bioSple:193,
internal_controls:5
}]
};
After:
var arr = [
{
"name": "AH5T5TAFXX-001",
"Bin_reads": [2436307,2906003],
"IC_lot": [1,1],
"LabChip_size_bp": [410,395],
"LibType": ["RNA", "RNA"],
"Tot_reads": [7386376,6680167]
"bioSple": [193,198],
"internal_controls": [5,5]
},{
"name": "AH5T5TAFXX-002",
"Bin_reads": [2436307,2906003],
"IC_lot": [1,1],
"LabChip_size_bp": [410,395],
"LibType": ["RNA", "RNA"],
"Tot_reads": [7386376,6680167]
"bioSple": [193,198],
"internal_controls": [5,5]
}
];