I have two objects, and I want to return them permuted in JavaScript , that is, if I enter a
and b
as arguments, I return [b,a]
, changing the order .
But I do not want a function that only returns the permuted result, but also that I change the values of the objects (they can also be arrays) entered.
In the following code, none of the two functions permutar
serves. The first does not, because it does not change the values, and only returns the permuted values. The second does not either, because although it returns the permuted values, it only changes the values within the function, but on the outside they remain the same:
var funciones_permutar=[
function permutar(a,b)
{
return [b,a]
},
function permutar(a,b)
{
var c=a
a=b
b=c
return [a,b]
}
]
for(var i=0;i<funciones_permutar.length;i++)
{
var objeto_1={d:2,e:3}
var objeto_2={f:4}
console.log("Función permutar "+(i+1))
//Devuelve correctamente.
console.log(JSON.stringify([objeto_1,objeto_2]))
console.log(JSON.stringify(funciones_permutar[i](objeto_1,objeto_2)))
//Devuelve [{"d":2,"e":3},{"f":4}], pero debería devolver [{"f":4},{"d":2,"e":3}]
console.log(JSON.stringify([objeto_1,objeto_2]))
}