1

I'm tring to convert a JSON object value to an integer and calculate.

It shows representativeRow.DTA_VAL well but the value of total shows NaN.

I don't think this works because the other code works well.

  datav=  Number(representativeRow.DTA_VAL);

this is my code

  var rows = resp.Sttsapitbldata[1].row;
        if (rows) {
            var representativeRow;
            for (i = 1; i < 30; i++) {
                representativeRow = rows[i];

                if(representativeRow.ITM_NM.substring(0,2)=="부산"){
                   // console.log(representativeRow.ITM_NM);
                    var sub =representativeRow.ITM_NM.substring(0,3);
                    var total;
                    var datav;
                    console.log(representativeRow.DTA_VAL);
                    datav=  Number(representativeRow.DTA_VAL);

                    total+=datav;
                    console.log(total);

                }

                itemNm2 = representativeRow.ITM_NM;
                dataV = representativeRow.DTA_VAL;
                //console.log(itemNm2);
                //console.log(dataV);
                options.data.data.push({locname: itemNm2, listshrs: dataV});
            }


            korea = webponent.visual.korea.init($(".korea"), style, options);

        }

See JSON file code below.

{"Sttsapitbldata":[{"head":[{"list_total_count":88},{"RESULT":{"CODE":"INFO-000","MESSAGE":"정상 처리되었습니다."}}]},{"row":[{"STATBL_ID":"T183673021266818","DTACYCLE_CD":"YY","WRTTIME_IDTFR_ID":"2016","ITM_ID":10001,"ITM_NM":"계","CLS_ID":50033,"CLS_NM":"강간","UI_NM":"명","DTA_VAL":5155,"DTA_SVAL":null},{"STATBL_ID":"T183673021266818","DTACYCLE_CD":"YY","WRTTIME_IDTFR_ID":"2016","ITM_ID":10002,"ITM_NM":"서울","CLS_ID":50033,"CLS_NM":"강간","UI_NM":"명","DTA_VAL":1129,"DTA_SVAL":null},{"STATBL_ID":"T183673021266818","DTACYCLE_CD":"YY","WRTTIME_IDTFR_ID":"2016","ITM_ID":10003,"ITM_NM":"부산","CLS_ID":50033,"CLS_NM":"강간","UI_NM":"명","DTA_VAL":314,"DTA_SVAL":null},
1
  • DTA_VAL seems to be 'null' in your array and null isn't a number. So maybe test for null and convert it to 0 (or whatever you need) first. Commented Dec 14, 2018 at 9:12

1 Answer 1

1

You should initialise total=0. You can't add to a null value on the first iteration. Although looking at it, I suspect you'd be wanting to initialise total outside of the loop

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.