-1
Hello,
I have an API that returns to me:
{
"resultSetMetadata": {
"count": 63,
"offset": 0,
"limit": 100
},
"results": [
{
"receivableBillId": 2048,
"installmentId": 1,
"conditionTypeId": "AT",
"dueDate": "2019-12-27",
"balanceDue": 0.0,
"generatedBoleto": true
},
{
"receivableBillId": 2048,
"installmentId": 2,
"conditionTypeId": "AT",
"dueDate": "2020-01-27",
"balanceDue": 1000,
"generatedBoleto": true
},
and so it goes.
I want a code return me the installmendId that meets the given conditions balanceDue > 0 and generatedBoleto = true. In this example the output of installmentId would be = 2
var response = JSON.parse(responseBody);
if (response.balanceDue > 0){
if (response.generatedBoleto = true){
numeroParcela = response.installmentId
}}
console.log(numeroParcela)
But when running this code, it gives the following error: Referenceerror: numeroParcela is not defined what is wrong ?
balanceDue
is a key inside objects of an array calledresults
within theresponse
. Soon,response.balanceDue
will always give Undefined because there is, and with that, the variablenumeroParcela
, that is within a condition that was not satisfied, does not exist when theconsole.log
is executed. Without citing the simple signal error=
withinif
.– Sam
How do I check the key values then ?
– joaogalhardo
You have to go through the array. It could be with a for.
– Sam
var Response = JSON.parse(responseBody); var numeroParcela = 0; var key = 0; for (key in Response) { if (Response[key].balanceDue > 1){ if (Response[key].generatedBoleto == false){ numeroParcela === = Response[key]. installmentId; }} ++key; } console.log(numeroParcela) ?
– joaogalhardo