I have a function that accepts destructed arguments (so that I can send named parameters from the calling code):
function createAccount({Param1, Param2, Param3, Param4}){
for (let [Key, Value] of Object.entries(arguments[0])) {
console.log(`Before transform ${Key}: ${Value}`);
// Param1: A, Param2: B, Param3: C, Param4: D
}
// Here I need to loop over the arguments and transform their values
let count = 0
for (let [Key, Value] of Object.entries(arguments[0])) {
arguments[0][Key] = count + 1;
count += 1;
}
for (let [Key, Value] of Object.entries(arguments[0])) {
console.log(`after transform ${Key}: ${Value}`);
// Param1: 1, Param2: 2, Param3: 3, Param4: 4
}
console.log(Param1, Param2, Param3, Param4);
// Param1: A, Param2: B, Param3: C, Param4: D
//It hasn't transformed?!
}
createAccount({Param1: 'A', Param2: 'B', Param3: 'C', Param4: 'D'});
I hope the code is self-explanatory but basically I am trying to transform the argument values from A, B, C, D respestively into 1, 2, 3, 4 which appears to work when I do the final for loop to output the Key: Value of arguments[0]. It does indeed show they are converted to numbers.
But why when I output the actual individual params it shows the original values of A, B, C, D?
According to the docs I should be able to assign values to the arguments object and it should update the params in the function? I think it has something to do with it being a destructed object I’m not sure.