I have the following object in a JS script I’m working on:
let time = {
years: 0,
months: 0
};
I also have a global helper function, on a seperate file, that calculates the total years and months given a number of months:
helpers.monthsToYears = function(months) {
var totalYears = months / 12;
return [Math.floor(totalYears), Math.round((totalYears - Math.floor(totalYears)) * 12)];
};
I want to update my time
array using the global function, but I am unsure as to how to do this simply.
I’ve searched and found ways to create a new object from a function’s output, but not how to update an existing object.
I have tried things like:
time[years,months] = helpers.monthsToYears(results.months);
but to no result.
Would anyone know the correct way to do this?
2
Answers
You could do
Depending on the meaning of "update an existing object" you are referring to, based on Zac Anger’s answer above, you can also perform: