I have a problem, I have an array that has certain null values, I would like to change all the null values to 0, I tried with .replace but it didn't work. I appreciate any help.
This is my code, I bring the original array from PHP, I would need to go through the array and replace the null values with 0
$.ajax({
url: 'STATS/metas.php',
type: 'GET',
dateType: 'text',
success: function(response){
console.log(response)
let metas = JSON.parse(response)
console.log(metas)
let cultivo = metas[0].totalCultivado
let cosecha = metas[0].totalCosechado
let diferencia = metas[0].diferencia
let metasChicas = metas[0].metasChicas
let metasMedianas = metas[0].metasMedianas
let cultivoKG = metas[0].KGcultivados
let cultivoATADO = metas[0].ATADOScultivados
let cultivoUNIDAD = metas[0].UNIDADEScultivadas
let cosechaKG = metas[0].KGcosechados
let cosechaATADO = metas[0].ATADOScosechados
let cosechaUNIDAD = metas[0].UNIDADEScosechadas
$('#m1').html(cultivo)
$('#kgCultivos').html(cultivoKG + " kg")
$('#atadoCultivos').html(cultivoATADO + " atados")
$('#unidadCultivos').html(cultivoUNIDAD + " unidades")
$('#m2').html(cosecha)
$('#kgCosechado').html(cosechaKG + " kg")
$('#atadoCosechado').html(cosechaATADO + " atados")
$('#unidadCosechado').html(cosechaUNIDAD + " unidades")
}
})
Apparently, it is not an array that has null values, but an object at position 0 of your array, so what you should do is use Object.keys, which returns all the keys of an object inside an array, and iterate over that array to replace the values in your object that are null.