Home  >  Article  >  Web Front-end  >  javascript determines whether key exists in json

javascript determines whether key exists in json

王林
王林Original
2023-05-10 09:39:065575browse

JavaScript is a widely used scripting language that supports the JSON data type. When processing JSON data, sometimes it is necessary to determine whether a certain key exists in a JSON object. This article will introduce methods and techniques on how to use JavaScript to determine whether a key exists in JSON.

1. JSON data type in JavaScript

JSON (JavaScript Object Notation) is a lightweight data format used for data exchange. In JavaScript, there are three JSON data types: objects, arrays and strings. Among them, the object is a collection of key-value pairs, the key is a string, and the value can be any JSON data type. The object is defined as follows:

var obj = {
    key1: "value1",
    key2: 2,
    key3: [1, 2, 3],
    key4: {
        subkey1: "subvalue1",
        subkey2: "subvalue2"
    }
};

where "key1" to "key4" are the attribute names of the object, which can be accessed using dot notation or square bracket notation, for example obj.key1 Both obj["key1"] can obtain attribute values.

2. Method to determine whether a key exists in a JSON object

  1. in operator

The in operator can be used to determine whether an object exists Attribute, the syntax is as follows:

key in object

Among them, key is the attribute name and object is the object. If the object has this attribute, returns true, otherwise returns false.

For example, to determine whether an object has a property named "key1":

var obj = {
    key1: "value1",
    key2: "value2"
};
if ("key1" in obj) {
    console.log("obj有key1属性");
} else {
    console.log("obj没有key1属性");
}
  1. hasOwnProperty method

hasOwnProperty method can be used to determine whether an object It has its own attributes, and the syntax is as follows:

object.hasOwnProperty(key)

Among them, key is the attribute name and object is the object. If the object has this attribute, returns true, otherwise returns false.

For example, to determine whether an object has a property named "key1":

var obj = {
    key1: "value1",
    key2: "value2"
};
if (obj.hasOwnProperty("key1")) {
    console.log("obj有key1属性");
} else {
    console.log("obj没有key1属性");
}
  1. typeof and undefined

In JavaScript, if you access an undefined If the property exists, undefined is returned. Therefore, you can use typeof and undefined to determine whether the attribute exists. The syntax is as follows:

typeof object.key !== "undefined"

where key is the attribute name and object is the object. If the object has this attribute, returns true, otherwise returns false.

For example, determine whether an object has an attribute named "key1":

var obj = {
    key1: "value1",
    key2: "value2"
};
if (typeof obj.key1 !== "undefined") {
    console.log("obj有key1属性");
} else {
    console.log("obj没有key1属性");
}

3. Conclusion

In JavaScript, determine whether a JSON object has a certain key There are three methods: in operator, hasOwnProperty method and typeof and undefined. Which method to use depends on the usage scenario and personal habits.

It is worth noting that when using the in operator and hasOwnProperty method, you should pay attention to the issue of inherited properties. If an object is not its own property, but is a property found in the prototype chain, both the in operator and the hasOwnProperty method will return false. Therefore, when using these two methods, you can use Object.prototype.hasOwnProperty.call(obj, key) in combination to determine whether a property is a property of the object itself.

4. Example

The following is a complete example that demonstrates how to use three methods to determine whether an object exists with a certain key:

var obj = {
    key1: "value1",
    key2: "value2"
};

// 方法1:in运算符
if ("key1" in obj) {
    console.log("方法1:in运算符,obj有key1属性");
} else {
    console.log("方法1:in运算符,obj没有key1属性");
}

// 方法2:hasOwnProperty方法
if (obj.hasOwnProperty("key1")) {
    console.log("方法2:hasOwnProperty方法,obj有key1属性");
} else {
    console.log("方法2:hasOwnProperty方法,obj没有key1属性");
}

// 方法3:typeof和undefined
if (typeof obj.key1 !== "undefined") {
    console.log("方法3:typeof和undefined方法,obj有key1属性");
} else {
    console.log("方法3:typeof和undefined方法,obj没有key1属性");
}

// 兼容继承属性
var Person = function() {};
Person.prototype.name = "Tom";
var p = new Person();
p.age = 20;

if ("name" in p) {
    console.log("兼容继承属性,p有name属性");
}

if (p.hasOwnProperty("name")) {
    console.log("兼容继承属性,p没有name属性");
}

if (Object.prototype.hasOwnProperty.call(p, "name")) {
    console.log("兼容继承属性,p没有name属性");
}

Through the above example, we can See the typical real-life applications of three methods for determining whether a key exists in JSON. We can choose different methods according to actual project needs, and continuously improve and learn in depth while optimizing the code.

The above is the detailed content of javascript determines whether key exists in json. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn