Search Array in JavaScript

前端 未结 2 417
遇见更好的自我
遇见更好的自我 2021-01-26 18:15

I need to sort through a data set which as you can see I\'ve assigned to the records variable. From that data I need to see if the zip code exists. If the zip code does not exis

2条回答
  •  孤街浪徒
    2021-01-26 18:43

    Well, you didn't exactly ask a question, but I'll answer anyway :) The answer is that you should not use a normal array for this, but rather a map or associative array. Fortunately a plain Javascript object can be used for this:

    var numbers = {};
    
    // Variables
    var records;
    var numbers;
    var index;
    var output;
    var outputMessageOne;
    var outputMessageTwo;
    var count = 0;
    
    output = document.getElementById('outputDiv');
    records = openZipCodeStudyRecordSet();
    
    output.innerHTML = "The unique zip codes are: ";
    
    while (records.readNextRecord()) {
    
        var zipCode = records.getSampleZipCode();
        numbers[zipCode] = 1; // just picking an arbitrary value
    }
    
    for (var zipCode: numbers) {
      output.innerHTML += zip + " ";
    }
    

    The reason is that this way you don't need to loop through the existing data for each new input.

提交回复
热议问题