Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda