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
autos[0].precio
the 0
represents the first object of the array and the key precio
and we assign it with the =
the new value.
Functional example
var autos= [{
"Modelo": "Mazda",
"Referencia": "Mazda 6",
"Precio": 73710
}];
var precioDescuento = 6120
autos[0].Precio = precioDescuento
console.log(autos);
regards I hope you help
Note : If your array has several objects, all you have to do is a for
to go through it and assign it the index
instead of the 0
something like this:
for (var i = 0; i < autos.length; i++) {
autos[i].Precio = precioDescuento
}
Functional example
var autos= [{
"Modelo": "Mazda",
"Referencia": "Mazda 6",
"Precio": 73710
},{
"Modelo": "corola",
"Referencia": "corola 2",
"Precio": 45221
},{
"Modelo": "chevrolet",
"Referencia": "chevrolet 9",
"Precio": 5090
}];
var precioDescuento = 6120
for (var i = 0; i < autos.length; i++) {
autos[i].Precio = precioDescuento
}
console.log(autos);
or you can also use the function forEach
to go through the array, it would be something like this
autos.forEach(function(item){
item.precio = precioDescuento
})
The forEach () method executes the indicated function once for each element in the array.
Syntax forEach
arr.forEach(function callback(currentValue, index, array) {
// tu iterador
}[, thisArg]);
Description
forEach () executes the callback function once for each element present in the array in ascending order. It is not invoked for indexes that have been eliminated or that have not been initialized (eg on arrays sparse)
var autos= [{
"Modelo": "Mazda",
"Referencia": "Mazda 6",
"Precio": 73710
},{
"Modelo": "corola",
"Referencia": "corola 2",
"Precio": 45221
},{
"Modelo": "chevrolet",
"Referencia": "chevrolet 9",
"Precio": 5090
}];
var precioDescuento = 6120
autos.forEach(function(item){
item.precio = precioDescuento
})
console.log(autos);