-1
Inside my Dashboard, the user makes a filter that goes to the database and returns a Javascript object through an asynchronous request and the array extension may vary, and may have one length
from 1 to 3, always!
So sometimes when the user filters, in my console returns the following:
And by clicking on the log, in this example, the key 2 of the array is underlined, indicating that this is the undefined one. I checked the database and everything and that’s it.
So for that I searched in the forum on the subject and built this function that is being used as in the image above:
function tratarValorIndefinido(vl){
if(typeof(vl) === "undefined"){
vl ="S/ HISTÓRICO";
}
};
Only instead of stamping the "S/ HISTORY", it is returning undefined
.
Where am I going wrong?
***HOW I CURRENTLY CARRY THE OBJ.
var dataBar = {
labels: [dataChart[0]['mesReferencia'],dataChart[1]['mesReferencia'],dataChart[2]['mesReferencia']],
datasets: [{
label: "CPF's Enviados",
backgroundColor: "rgba(0,51,90,0.8)",
borderColor: "rgba(0,51,90,0.9)",
borderWidth: 2,
hoverBackgroundColor: "rgba(0,51,90,0.9)",
hoverBorderColor: "rgba(0,51,90,1)",
data: [dataChart[0]['cpfsEnviados'],dataChart[1]['cpfsEnviados'],dataChart[2]['cpfsEnviados']]
},
**structure of data[0]
:
So it gets simpler and should solve the problem:
function tratarValorIndefinido(vl){ return vl || "S/ HISTÓRICO"; }
– André Luan
typeof(vl)
is wrong syntax. You can show how you are usingtratarValorIndefinido
so we can help with the right code?– Sergio
Fixing: What is undefined is not the integer element 2 of the array you are trying to access. What would solve the problem be: dataChart[2] ? dataChart[2]["mesReference"]: "S/HISTORY"
– André Luan
@jvbarsou , try the same condition without using quotes [ typeof(v1) === Undefined ], because if you put inside quotes it looks like a string "any text" to be compared.
– Wilson Rosa Gomes