Replace Undefined javascript value

Asked

Viewed 255 times

-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:

inserir a descrição da imagem aqui

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. inserir a descrição da imagem aqui

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]:

inserir a descrição da imagem aqui

  • So it gets simpler and should solve the problem: function tratarValorIndefinido(vl){ return vl || "S/ HISTÓRICO"; }

  • typeof(vl) is wrong syntax. You can show how you are using tratarValorIndefinido so we can help with the right code?

  • 2

    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"

  • @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.

2 answers

2

Every Javascript function that does not declare a return with the keyword return, returns undefined by default.

I think that’s all that’s missing. return vl; at the end of the function should solve your problem. Or, return "S/ HISTÓRICO" straightforward.

  • Following his example, it does not even change to Undefined the value

  • 3

    @jvbarsou it is unfair to correct answers without showing where and how you use your code. Click on editar on the question to show how you’re using that code.

1

I would suggest you go through the list itself and if you find undefined change the direct value on the object. An example of the list can be:

[
 {id: 1, mesReferencia: "Jan"},
 {id: 2, mesReferencia: undefined}
];

And use the Array#foreach to modify the value:

arrayDeObjetos.forEach(function(item) {
  if (typeof item.mesReferencia === "undefined") item.mesReferencia = "S/ HISTÓRICO";
});

Example:

var itens = [
 {id: 1, mesReferencia: "Jan"},
 {id: 2, mesReferencia: undefined}
];

itens.forEach(function(item) {
  if (typeof item.mesReferencia === "undefined") item.mesReferencia = "S/ HISTÓRICO";
});
  
console.log(itens);

  • in case, can be placed as in the image example? I edited the code to show how it is currently brought.

  • I just entered @Ucas

  • I will edit in response the structure

Browser other questions tagged

You are not signed in. Login or sign up in order to post.