I have this fix
var autos= [{
"Modelo": "Mazda",
"Referencia": "Mazda 6",
"Precio": 73710
}];
and I have a variable
var precioDescuento = 6120
What I am trying to do is replace the price of 73710 with the price of 6120
I was trying to do it this way but I get an error, any ideas?
autos.push([ "Precio": precioDescuento ]);
console.log(autos);
The end result would be
var autos= [{
"Modelo": "Mazda",
"Referencia": "Mazda 6",
"Precio": 6120
}];
What you should do is first of all go through your
array
(since what you have is anarray
object) and overwrite the data creating a new onearray
with them. It is necessary to make a condition to select the property that you want to change of any of the objects contained inarray
it in case you do not want it to apply to all objects.I added a new object so you can better see how the code works:
I hope it is what you are looking for.
To change a value of an array is simply to put the key in this case it represents
autos[0].precio
the0
first object of the array and the keyprecio
and we assign=
the new value to it.functional example
Greetings, I hope it helps you.
functional example
or you can also use the function
forEach
to go through the array it would be something like thissyntax forEach
Description