Search Array in JavaScript

前端 未结 2 419
遇见更好的自我
遇见更好的自我 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:44

    You can simplify your for loop like so:

     matchedZip = false;
    
     for(i in numbersArray) {
        if (numbersArray[i] === zipCode) {
           matchedZip = true;
        }
     }
    
     if ( ! matchedZip) {
        numbersArray.push(zipCode);
     }
    

    Try plugging that into your while loop. If you have the array push inside of the for loop you're going to end up pushing each zip code in every time there is not a match.

提交回复
热议问题