


How to determine whether a field exists in the passed JSON data in JS_javascript skills
How to determine whether a certain field exists in the JSON data passed in?
1.obj["key"] != undefined
This is defective. If the key is defined and the value is very often undefined, then there will be a problem with this sentence.
2.!("key" in obj)
3.obj.hasOwnProperty("key")
These two methods are better and recommended.
Original answer:
Actually, checking for undefined-ness is not an accurate way of testing whether a key exists. What if the key exists but the value is actually undefined?
var obj = { key: undefined };
obj["key"] != undefined // false, but the key exists!
You should instead use the in operator:
"key" in obj // true, regardless of the actual value
If you want to check if a key doesn't exist, remember to use parenthesis:
!("key" in obj) // true if "key" doesn't exist in object
!"key" in obj // ERROR! Equivalent to "false in obj"
Or, if you want to particularly test for properties of the object instance (and not inherited properties), usehasOwnProperty:
obj.hasOwnProperty("key") // true

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to parse incoming JSON data using request body in FastAPI FastAPI is a modern Python-based web framework that provides rich functionality and high-performance asynchronous support. When using FastAPI to handle HTTP requests, it is often necessary to parse the incoming JSON data. This article will introduce how to use the request body to parse incoming JSON data in FastAPI and provide corresponding code examples. Import dependencies First, we need to import FastAPI dependencies and JS

How do PHP and MySQL handle JSON data? In modern web applications, JSON (JavaScriptObjectNotation) is often used to store and exchange data. PHP and MySQL are two commonly used technologies, and how to use them to process JSON data is a common question. This article will introduce how to use PHP and MySQL to process JSON data and provide some code examples. 1. Export data from MySQL to JSON First, let’s

PHP is a scripting language widely used in website development. For developers, it is often necessary to determine whether a field is empty. In PHP, determining whether a field is empty can be achieved through some simple methods. This article will introduce how to determine whether a field is empty in PHP, and provide specific code examples for your reference. In PHP, you can usually use the empty() function or isset() function to determine whether a field is empty. Next, we introduce the usage of these two functions respectively. Use the empty() function

NoSuchFieldError in Java - Solution to field not found Java is a high-level programming language that is widely used in enterprise-level applications and large-scale data processing. During the development process of Java, errors such as NoSuchFieldError may occur. This error means that the JVM cannot find the required field at runtime. In this article, we will take a deeper look at NoSuchFieldError and how to resolve it. What is NoSuchFieldE

A mysql field is a column of a specific type and length in a mysql database table that is used to store data. In MySQL, each field must have a specific data type. Common data types include integers, floating point numbers, strings, dates, and times. These data types determine the data that MySQL can store in each field.

Methods to add fields in the table: 1. Use the "ALTER TABLE table name ADD new field name data type;" statement to add fields at the end; 2. Use the "ALTER TABLE table name ADD new field name data type FIRST;" statement to add it at the beginning Field; 3. Use the "ALTER TABLE table name ADD new field name data type [constraints] AFTER existing field name;" statement to add a field in the middle.

PHP method to determine whether a field is not empty: 1. Use the isset method to determine whether the variable has been initialized; 2. Use the empty method to detect whether the variable is "empty"; 3. Use the "var == null" method to determine whether the variable is "empty" ; 4. Use the is_null method to detect whether the variable is "null"; 5. Use the "var === null" method to detect whether the variable is "null", and the type of the variable must also be "null".

How to use Vue form processing to hide and display elements of form fields Introduction: In front-end development, processing forms is a common and important task. Sometimes we need to hide and display form field elements based on user selection or other conditions. It is very simple to implement this function in Vue. This article will introduce how to use Vue form processing to hide and display elements of form fields, and attach code examples for reference. 1. Use the v-if instruction to hide and display the v-if instruction in Vue according to
