Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
how to check if key is exist in data or not

Hi all I have following data of zip codes:

Now I am trying to do following. I have input and I am writing some zip code, I need to check if that zip code is exist in my data of zip codes I should set my state true if not exist then false.

here is my code:

const zipCode = {
  2101: [
    {
      City: "Boston",
      State: "Massachusetts",
      Delivery: "Yes",
      Address: ""
    }
  ],
  2108: [
    {
      City: "Boston",
      State: "Massachusetts",
      Delivery: "Yes",
      Address: ""
    }
  ],
  2109: [
    {
      City: "Boston",
      State: "Massachusetts",
      Delivery: "Yes",
      Address: ""
    }
  ],
  2110: [
    {
      City: "Boston",
      State: "Massachusetts",
      Delivery: "Yes",
      Address: ""
    }
  ],
  2111: [
    {
      City: "Boston",
      State: "Massachusetts",
      Delivery: "Yes",
      Address: ""
    }
  ],
  2112: [
    {
      City: "Boston",
      State: "Massachusetts",
      Delivery: "Yes",
      Address: ""
    }
  ],
  2113: [
    {
      City: "Boston",
      State: "Massachusetts",
      Delivery: "Yes",
      Address: ""
    }
  ],
 
};

export default zipCode;

    export default function App() {
     const [valid, setValid] = useState(null);
     const [zipCodefromInput, setZipCodeFromInput] = useState();

      const checkZipCode = (e) => {
       setZipCodeFromInput(+e.taget.value);

       Object.keys(zipCode)
        .filter((key) => zipCodefromInput.includes(key))
        .reduce((obj, key) => {
        obj[key] = zipCode[key];
        // setValid(true) else setValid(false) ???
       }, {});
     };

  console.log(valid);

  return (
    <div className="App">
      <input onChange={checkZipCode} />

     
    </div>
  );
}

But it not works for me, please help me to correct my code.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Returns true when key exists.

const zipExists = (key) => key in zipCode;

More info: Checking if a key exists in a JavaScript object?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!