Playing with JavaScript objects

Tram Ho

1. Delete 1 property of 1 Object

There are many ways to delete an object property as above

2. Check if the Object has that key or not?

The uncertainty test is not the correct way to check if a key exists or not. What if the key exists but the value is undefined?

There are also many ways that an exact test can be done

The first can be mentioned is the print operator, remember to use parentheses otherwise it will error

Or you can use hasOwnProperty

3. Size of an Object

The most likely answer would be

This is an update as of 2016 and widely deployed ES5 and above. For IE9 + and all other modern ES5 + supported browsers, you can use Object.keys () to make the code above become:

This does not have to modify any existing prototypes as Object.keys () is now built-in. Objects can have symbolic properties that cannot be returned through the Object.key method. Therefore, the answer would be incomplete without mentioning them. Symbol types have been added to the language to create unique identifiers for object properties. The main benefit of symbol type is to prevent overwriting.

Object.keys or Object.getOwnPropertyNames do not work with the symbol property. To return them you need to use Object.getOwnPropertySymbols.

4. Conditionally add properties to the object?

To add properties to an object depending on conditions, we can use the following

The same can be used with the React component

Share the news now

Source : Viblo