Navigate back to the homepage

How to check if a json object has a key in typescript?

Infinitbility
TypeScript
November 30th, 2021 · 1 min read
How to check if a json object has a key in typescript?

Hello Friends 👋,

Welcome To Infinitbility! ❤️

TypeScript almost has the same syntax as javascript to check object has a key or not, in this tutorial, we will learn multiple ways to check object has a key or not in typescript.

Now, we are going to learn below three ways to check object keys exist or not.

  1. hasOwnProperty() method
  2. if..else method
  3. () ? : ; ternary method

Let start today’s tutorial How to check if a JSON object has a key in typescript?

Before we move to check all three methods with examples, let first create an example object which we can use in all the below examples.

1interface userInterface {
2 id: number,
3 name: string,
4};
5
6let user: userInterface = { id: 1, name: 'infinitbility' };

hasOwnProperty() method

When inheritance is an important part of the structure of your application, the difference is that it will result in true even for properties inherited by parent objects. hasOwnProperty() doesn’t. It will only return true if the object has that property directly - not one of its ancestors.

1interface userInterface {
2 id: number,
3 name: string,
4};
5
6let user: userInterface = { id: 1, name: 'infinitbility' };
7
8
9user.hasOwnProperty("id");
10user.hasOwnProperty("example");

Output

hasOwnProperty, typescript, Example

if..else method

if..else also can handle to check key exists or not in objects.

Here is a list of falsy values:

  • false
  • 0 (zero)
  • -0 (negative zero)
  • 0n (BigInt zero)
  • "", '', `` (empty string)
  • null
  • undefined
  • NaN (not a number)
1interface userInterface {
2 id: number,
3 name: string,
4};
5
6let user: userInterface = { id: 1, name: 'infinitbility' };
7
8if(user.id){
9 true;
10} else {
11 false;
12}
13
14if(user.example){
15 true;
16} else {
17 false;
18}

Output

if..else, typescript, Example

() ? : ; ternary method

Ternary method work like the same if..else method, but you can use it as a shorthand.

1interface userInterface {
2 id: number,
3 name: string,
4};
5
6let user: userInterface = { id: 1, name: 'infinitbility' };
7
8(user.id) ? true : false;
9(user.example) ? true : false;

Output

ternary, typescript, Example

Thanks for reading…

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to create a border in react native?

How to create a border in react native?

React Native border example

November 29th, 2021 · 1 min read
How to sort an array of objects by a property value in typescript?

How to sort an array of objects by a property value in typescript?

Example of sorting array of objects in typescript.

November 28th, 2021 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]