6 Ways to Elegantly Handle Objects in JavaScript

6 Ways to Elegantly Handle Objects in JavaScript

Preface

Like other programming languages, JavaScript has its own data types such as numbers, strings, arrays, objects, etc. Objects are a very important data type in JavaScript. They have many useful methods that can be used to easily handle objects in daily project development.

This article introduces 6 methods that can be used in projects. Take this opportunity to deepen your understanding of their usage.

1. Object.freeze()

Object.freeze() method prevents the data in an object from being modified, that is, it freezes an object so that properties cannot be added, updated, or deleted from the object.

const author = {
    name: "Quintion",
    city: "Shenzhen",
    age: 18,
    validation: true,
};

Object.freeze(author);

author.name = "QuintionTang";
author.province = "Guangdong";
delete author.age;
console.log(author); // { name: 'Quintion', city: 'Shenzhen', age: 18, validation: true }

As shown in the code above, the attribute name is updated, the attribute province is added, and the attribute age is deleted, but there is no change in the final object.

2. Object.seal()

Object.seal() method is somewhat similar to Object.freeze() . Prevents adding new properties to an object and removing properties, but allows changing and updating existing properties.

const author = {
    name: "Quintion",
    city: "Shenzhen",
    age: 18,
    validation: true,
};

Object.seal(author);

author.name = "QuintionTang";
author.province = "Guangdong";
delete author.age;
console.log(author); // { name: 'QuintionTang', city: 'Shenzhen', age: 18, validation: true }

As you can see from the above code, adding new attributes and deleting attributes are invalid, only updating the attribute name is effective.

3. Object.keys()

Object.keys() method returns an array containing the names of all the keys of the parameter object. The order of the attribute names in the array is consistent with the order returned when traversing the object normally.

Take a look at the following code:

const author = {
    name: "Quintion",
    city: "Shenzhen",
    age: 18,
    validation: true,
};

console.log(Object.keys(author)); // [ 'name', 'city', 'age', 'validation' ]

You can see that the result printed in the above code is an array containing the keys as output. The output results can be processed or iterated using array methods.

console.log(Object.keys(author).length); // 4

4. Object.values()

Object.values() is similar to Object.keys() , but Object.values() gets the values ​​of all attributes in the object and returns an array of values.

const author = {
    name: "Quintion",
    city: "Shenzhen",
    age: 18,
    validation: true,
};

console.log(Object.values(author)); // [ 'Quintion', 'Shenzhen', 18, true ]

5. Object.create()

Object.create() creates a new object based on the prototype __proto__ of an existing object. Let's look at the following code:

const author = {
    firstName: "Quintion",
    lastName: "Tang",
    fullName() {
        return `${this.firstName} ${this.lastName}`;
    },
};

const newAuthor = Object.create(author);
console.log(newAuthor); // {}
newAuthor.firstName = "Ronb";
newAuthor.lastName = "Joy";
console.log(newAuthor.fullName()); // Ronb Joy

In the above code, object. create() is used to create a new object newAuthor with the prototype of author object. In this way, in the new object newAuthor you can change the corresponding attribute values ​​​​just like changing the attribute values ​​​​of author object. Doesn’t this look a bit like inheritance? Yes, class inheritance can be achieved using Object.create .

6. Object.entries()

Object.entries() allows you to get the keys and values ​​of an object, returning a multidimensional array where each dimension contains each key and value, such as [鍵, 值]

const author = {
    firstName: "Quintion",
    lastName: "Tang",
    fullName() {
        return `${this.firstName} ${this.lastName}`;
    },
};

console.log(Object.entries(author));

The output is as follows:

[
  [ 'firstName', 'Quintion' ],
  [ 'lastName', 'Tang' ],
  [ 'fullName', [Function: fullName] ]
]

Summarize

This article briefly introduces the six common methods of objects and provides corresponding sample codes. In the actual process of coding and processing objects, using the above methods can make the code more elegant.

This concludes this article about 6 ways to elegantly process objects in JavaScript. For more content related to JavaScript processing objects, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • 3 ways to create JavaScript objects
  • JavaScript Dom Object Operations
  • Five ways to traverse objects in javascript Example code

<<:  The difference and usage of single-line and double-line layout in Flex mobile layout

>>:  Solution to the problem that MySQL commands cannot be entered in Chinese

Recommend

Install CentOS system based on WindowsX Hyper-V

At present, most people who use Linux either use ...

A detailed introduction to the netstat command in Linux

Table of contents 1. Introduction 2. Output Infor...

Teach you how to use webpack to package and compile TypeScript code

TypeScript Bundling webpack integration Usually, ...

Detailed steps to build the TypeScript environment and deploy it to VSCode

Table of contents TypeScript environment construc...

Common problems and solutions during MySQL MGR construction

Table of contents 01 Common Faults 1 02 Common Fa...

idea combines docker to realize image packaging and one-click deployment

1. Install Docker on the server yum install docke...

How to use nginx to configure access to wgcloud

The nginx configuration is as follows: Such as ht...

A brief introduction to MySQL database optimization techniques

A mature database architecture is not designed wi...

Complete code for implementing the vue backtop component

Effect: Code: <template> <div class=&quo...

Implementing parameter jump function in Vue project

Page Description:​ Main page: name —> shisheng...

MySQL spatial data storage and functions

Table of contents 1. Data Type 1. What is MySQL s...

Detailed explanation of SELINUX working principle

1. Introduction The main value that SELinux bring...

Solution to mysql error code 1064

If the words in the sql statement conflict with t...