Replace data in an array

0

I have this arrangement

var autos= [{
    "Modelo": "Mazda",
    "Referencia": "Mazda 6",
    "Precio": 73710
}];

and I have a variable

var precioDescuento = 6120

What I'm trying to do is replace the price of 73710 with 6120

I was trying to do it this way but I get an error, any ideas?

autos.push([ "Precio": precioDescuento ]);

console.log(autos);

The final result would be

var autos= [{
        "Modelo": "Mazda",
        "Referencia": "Mazda 6",
        "Precio": 6120
    }];
    
asked by Eduard 19.12.2017 в 14:05
source

2 answers

2

What you must do is first of all go through your array (since what you have is a array of objects) and overwrite the data creating a new array with them. It is necessary to make a condition to select the property you want to change from one of the objects contained in array 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:

var autos = [{
    "Modelo": "Mazda",
    "Referencia": "Mazda 6",
    "Precio": 73710
}, {
    "Modelo": "Audi",
    "Referencia": "R8",
    "Precio": 73710
}];

var precioDescuento = 6120;
var modelo = "Mazda";

autos.map(function(dato){
  if(dato.Modelo == modelo){
    dato.Precio = precioDescuento;
  }
  
  return dato;
});

console.log(autos)
    
answered by 19.12.2017 / 14:16
source
1

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);
    
answered by 19.12.2017 в 14:19