skip to Main Content
var data_filter = data.filter( element => element.zip == "12180")
console.log(data_filter)

I have this so far which gets me 1 zip code match but I have 126, how do I rewrite this to return results for all entries that contain any of those 126 zip codes

I’m not a developer, just trying to run some reports on data in our system

2

Answers


  1. The easiest way to accomplish your task would be to start by creating an array of the 126 zip codes, so there’s something to compare to. If there’s any useful pattern to it you could use that to generate the list, but it’s probably going to need to be done by hand.

    Once you have the list, you can use something like:

    const zipCodes = ["12180"]
    
    const filtered = data.filter(element => zipCodes.includes(element.zip))
    
    console.log(filtered)
    

    Note that the zip code in the array zipCodes is a string and not a number, so that the string element.zip will match it when Array.includes goes searching.

    Login or Signup to reply.
  2. Do you want to use filters to filter out the data you specify?

    let zip = "12180";
    const data_filter = (zip) =>{
      return data.filter( element => element.zip == zip)
    }
    console.log(data_filter(zip));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search