Home>

After checking whether it is included in the zip code list in csv with Java Script, we are creating a script that displays the address corresponding to that zip code.

Because it is done with a for statement, the results corresponding to all CSVs are displayed and a bug appears.
I tried.match ()in addition to the for statement, but it didn't work.

<input maxlength = "7" type = "text" placeholder = "1234567">
          <button>Verify</div>
$('# search'). click (function () {
   var csvList;
   var capital = $('# zip'). val ();
   var count = (String (capital) .length);
   console.log ('capital:', capital);
   console.log ('count:', count);
   if (count === 7) {
     var a = capital.slice (0,3);
     var b = capital.slice (-4);
     var zipcode = a + '/' + b;
     console.log ('zipcode:', zipcode);
     // read csv
     $.ajax ({
       url: 'area.csv',
       success: function (data) {
         csvList = $.csv () (data);
         console.log ('csvList:', csvList);
         for (var i = 1;i<csvList.length;i ++) {
           if (csvList [i] [0] .indexOf (capital)! == -1) {
             // if zip code is found
             let postFetch = `https://madefor.github.io/postal-code-api/api/v1/${zipcode} .json`
             postFetch = `https://cors-allow.azurewebsites.net/?url=${postFetch}`;
             console.log ('postFetch:', postFetch);
             $.getJSON (postFetch)
               .done (function (data) {
                 // display search results
                 console.log ('Display search results');
                 var result = data.data [0];
                 console.log ('result', result);
                 var target = document.getElementById ('result');
                 target.innerHTML = "<p>Postal code is registered." + result.en.prefecture = "</p>";
               })
             } else if (csvList [i] [0] .indexOf (capital)! == -1) {
               // If the postal code is not found
               console.log ('Display results not specified');
               let postFetch = `https://madefor.github.io/postal-code-api/api/v1/${zipcode} .json`
               postFetch = `https://cors-allow.azurewebsites.net/?url=${postFetch}`;
               console.log ('postFetch:', postFetch);
               $.getJSON (postFetch)
                 .done (function (data) {
                   // display search results
                   var result = data.data [0];
                   console.log ('result', result);
                   var target = document.getElementById ('result');
                   target.innerHTML = "<p>Zip code not registered.</p>";
                 })
             }
           }
         },
         error: function (XMLHttpRequest, textStatus, errorThrown) {
           var target = document.getElementById ('result');
           target.innerHTML = "<div><p>Please enter the correct zip code</p></div>";
         }
       })
     } else {
       // if not 7 characters
       console.log ('not 7 characters');
       var target = document.getElementById ('result');
       target.innerHTML = "<div><p>Please enter the correct zip code</p></div>";
     }
   });

I'm sorry, but please help me,

  

console.log ('csvList') result

  • Answer # 1

    The find () method or findIndex () method may be fine
    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find
    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

Related articles