Home >Web Front-end >JS Tutorial >How to determine whether there are parameters in an object in javascript

How to determine whether there are parameters in an object in javascript

王林
王林Original
2021-10-28 17:40:204166browse

Javascript method to determine whether there are parameters in the object: use the hasOwnproperty() method, such as [var obj = {a: 1,b: 2,c: 3} console.log(obj.hasOwnProperty( 'a'));].

How to determine whether there are parameters in an object in javascript

#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.

Suppose there is an object Object with many parameters in it, but now we need to determine whether a certain parameter exists in the current object. What should we do?

It’s actually very simple. Just use the hasOwnproperty() method. Let’s take a look!

var obj = {
            a: 1,
            b: 2,
            c: 3
        }
        console.log(obj.hasOwnProperty('a')); // true
        因为当前定义对象中含有此参数, 故返回值为 true
var obj = {
            a: 1,
            b: 2,
            c: 3
        }
        console.log(obj.hasOwnProperty('asd')); // false
        此时返回的就是false 因为当前对象中不包含此值,

Its syntax can also be seen obj.hasOwnProperty(prop)

All objects that inherit Object will inherit the hasOwnProperty method. This method can be used to check whether an object contains specific properties of its own; unlike the in operator, this method ignores properties inherited from the prototype chain.

Recommended learning: javascript video tutorial

The above is the detailed content of How to determine whether there are parameters in an object in javascript. 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