The issue you're running into is a problem with mutability, in your code data and template actually reference the same object and therefore changes to one will affect the other. You should make an effort to not mutate your variables and instead return a new object when you need to change something with them. You'll want to use the Object.assign()
or spread operator. Another issue arises from the fact that there's not a proper way to delete object keys from JS the delete
function you're using is actually quite slow.
const jsonFunction = input => {
return {
...input,
candidates: null,
...input
}
};
const data = jsonFunction(template);
or alternatively with Object.assign()
const jsonFunction = input => {
return Object.assign({}, input, candidates: null);
}
const data = jsonFunction(template);
This will return a new object and assign it to data, by handling it immutably it means you can deal with data
and template
without them affecting each other.