I have encountered a problem where I am able to get the output, but I am struggling to figure out how to change certain elements. Specifically, I have the total salary but I am unsure how to modify the keys and achieve the desired format.
The desired output format is as follows:
{
"433": {
employee_name: 'Rishi',
total_salary: 5200
},
"434": {
employee_name: 'pathak',
total_salary: 5600
},
"435": {
employee_name: 'partap',
total_salary: 5700
}
}
function takeInput(output){
let total = [];
let totalSalary = [];
for(let i=0; i< output.length; i++){
if(typeof(output[i].employee_data.salary[0]) == "object"){
total[i] = output[i].employee_data.salary[0];
totalSalary[i] = addSalary(total[i]);
output[i].employee_data.salary[0] = addSalary(total[i]);
console.log(output[i]);
}
}
}
function addSalary(income){
let total = 0;
let values = Object.values(income)
for(let i=0; i< values.length; i++){
total = total + values[i];
}
return total;
}
let output = [
{
employee_data: {
employee_name: 'Rishi',
id: "433",
salary: [{'march': 1200, 'april': 2000, 'may': 2000}]
}
},
{
employee_data: {
employee_name: 'pathak',
id: "434",
salary: [{'march': 1100, 'april': 2200, 'may': 2300}]
}
},
{
employee_data: {
employee_name: 'partap',
id: "435",
salary: [{'march': 1200, 'april': 2200, 'may': 2300}]
}
}
]