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. :)
– Luiz Felipe
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.
– Henqsan