I have an array of objects containing a property called 'memory' whose value is in MB. I need to convert the array containing the object with 'memory' value in GB. How can I do it using Underscore.js.
Below is the code:
var parseSize = function(obj){
if (obj === 0 ){
return 0;
} else {
return parseFloat(obj/1024).toFixed(2);
}
}
var testData=[
{ name: 'ddd',Vcpu: 2, memory: 4096, os: 'Microsoft Windows Server 2008 (32-bit)'},
{ name: 'eee',Vcpu: 2, memory: 2040, os: 'Microsoft Windows Server 2008 (32-bit)'},
{ name: 'ddd',Vcpu: 2, memory: 4096, os: 'Microsoft Windows Server 2008 (32-bit)'},
{ name: 'eee',Vcpu: 2, memory: 2040, os: 'Microsoft Windows Server 2008 (32-bit)'}
];
testData =_.invoke(testData , function(){
testData['memory'] = parseSize(testData['memory']) + " GB";
});
console.log(testData);
The above code is not working. PLease let me know where I am going wrong.
Adding the Jsfiddle link: http://jsfiddle.net/prashdeep/k29zuba2/