Ask Your Question
0

Determine whether a JSON object contains a specific key.

asked 2022-10-04 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-06-22 01:00:00 +0000

plato gravatar image

To determine whether a JSON object contains a specific key, you can use the hasOwnProperty() method in JavaScript.

Here's an example:

const jsonObj = {
   name: 'John',
   age: 25,
   city: 'New York'
};

if (jsonObj.hasOwnProperty('age')) {
   console.log('JSON object has the key "age"');
} else {
   console.log('JSON object does not have the key "age"');
}

In this example, we have a JSON object jsonObj with three keys: name, age, and city. We use the hasOwnProperty() method to check if the object has the key 'age', and based on the result we log the appropriate message to the console.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-10-04 11:00:00 +0000

Seen: 11 times

Last updated: Jun 22 '22