Testing whether a key contains a value within a json

Asked

Viewed 275 times

0

I’m trying to verify that in a particular key of a json there is a value, but I’m not getting it. I’ve tried using includes, indexof, some, among other javascript functions.

I have an array with months:

const MONTHS = ['Jan', 'Fev', 'Mar', 'Abr', 'Maio', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'];

From a database query I have the result of a query that sums records from a table column and groups them in months. Basically the returned json has the following structure:

params

{
    "open": {
        "mes": "Abr",
        "total": "56.00"
    },
    "pay": null,
    "cancel": null
}

In the above structure was returned only the month of Abr, however, if it had more records and these records contained the remaining months they would be returned as well. So at some point I might have 7 months with data and another 5 with no data, for example. In javascript I am trying to popular the respective arrays to include them in a graph:

var data_open = [];
var data_pay = [];
var data_cancel = [];

$.each(params, function(key_param, value_param) {
        if (key_param == 'open') {
            $.each(MONTHS, function(key_open, value_open){
                if (MONTHS.indexOf(value_param.mes) > -1 && value_open == value_param.mes) {
                    data_open.push(value_param.total);
                } else {
                    data_open.push(0);
                }
            });
        } else if (key_param == 'pay') {
            $.each(MONTHS, function(key_pay, value_pay){                
                if (MONTHS.indexOf(value_param.mes) > -1 && value_pay == value_param.mes) {
                    data_pay.push(value_param.total)
                  } else {
                    data_pay.push(0);
                  }
            });
        } else if (key_param == 'cancel') {
            $.each(MONTHS, function(key_cancel, value_cancel){                
                if (MONTHS.indexOf(value_param.mes) > -1 && value_cancel == value_param.mes) {
                    data_cancel.push(value_param.total);
                  } else {
                    data_cancel.push(0);
                  }
            });
        }
    });

The point is that when the json key has the keys month and total the algorithm can popular the array inside the if, for example the key open json has records, but the keys pay and Cancel no, and in those that have no records the algorithm can not continue the execution.

How can I check if the keys have record, and include the logic to continue the execution and popular the arrays with the values 0.00?

RESOLUTION

const MONTHS = ['Jan', 'Fev', 'Mar', 'Abr', 'Maio', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'];

const json_data = {
    "open": {
        "mes": "Abr",
        "total": "56.00"
    },
    "pay": null,
    "cancel": null
}

var data_open = [];
var data_pay = [];
var data_cancel = [];

$.each(json_data, function(key_param, value_param) {
        if (key_param == 'open') {
            $.each(MONTHS, function(key_open, value_open){
                if (value_param && value_param.hasOwnProperty('mes') && MONTHS.indexOf(value_param.mes) > -1 && value_open == value_param.mes) {
                    data_open.push(value_param.total);
                } else {
                    data_open.push(0);
                }
            });
        }else if (key_param == 'pay') {
            $.each(MONTHS, function(key_pay, value_pay){                
                if (value_param && value_param.hasOwnProperty('mes') && MONTHS.indexOf(value_param.mes) > -1 && value_pay == value_param.mes) {
                    data_pay.push(value_param.total)
                  } else {
                    data_pay.push(0);
                  }
            });
        }else if (key_param == 'cancel') {
            $.each(MONTHS, function(key_cancel, value_cancel){                
                if (value_param && value_param.hasOwnProperty('mes') && MONTHS.indexOf(value_param.mes) > -1 && value_cancel == value_param.mes) {
                    data_cancel.push(value_param.total);
                  } else {
                    data_cancel.push(0);
                  }
            });
        }
    });
    
    $.each(data_open, function(key, value){
       document.getElementById('result_open').innerHTML += '<p>' + key + " - " + value + '</p>';
    });
    
    $.each(data_pay, function(key, value){
       document.getElementById('result_pay').innerHTML += '<p>' + key + " - " + value + '</p>';
    });
    
    $.each(data_cancel, function(key, value){
       document.getElementById('result_cancel').innerHTML += '<p>' + key + " - " + value + '</p>';
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>ORDENS ABERTAS</p>
<div id="result_open"></div>
<p>ORDENS PAGAS<p>
<div id="result_pay"></div>
<p>ORDENS CANCELADAS</p>
<div id="result_cancel"></div>

  • I still don’t quite understand what you want to do. Maybe it’s wise to add an example of what you want as entree in your script and what you want as exit. :)

  • I want to popular the "data_open", "data_pay" and "data_cancel" arrays with the data that is in json. Only when a json key is empty/null the algorithm cannot process the code, I believe it is because in ifs the key check is not correct. In the example I posted, the "open" json key contains data and in this case the algorithm processes the verification and continues with the code, but in the keys where there is no data, such as the "pay" and "Cancel" key, this does not happen.

1 answer

0


If I understand your question correctly, you want to check if an object has a certain property, and you need it because there is an error in your code in the excerpt MONTHS.indexOf(value_param.mes) within the if "pay" and "Cancel" on account of these items being null in your json.

To fix the problem first you should check if value_param is different from null, then you can use the function hasOwnProperty to check if your object has the property in question. Your final code will look similar to the following:

if (value_param && value_param.hasOwnProperty('mes') && MONTHS.indexOf(value_param.mes) > -1 && value_pay == value_param.mes) {
   // seu código
}
  • Thank you, now executed correctly!

Browser other questions tagged

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