Detailed explanation of various ways to merge javascript objects

Detailed explanation of various ways to merge javascript objects

Various ways to merge objects (extremely useful when obtaining data through an interface and assigning it to a local object)

The first method: manual assignment (very good)

const obj1 = {
  name: "zs",
  age: 13,
};
const obj2 = {
  name: "ls",
  sex: "female",
};
obj1.name = obj2.name;
obj1.sex = obj2.sex;

This method is the simplest, but in daily projects, an object has many properties, so it would be a bit cumbersome if this method is still used.

Second: Extension operator

const obj1 = {
  name: "zs",
  age: 13,
};
const obj2 = {
  name: "ls",
  sex: "female",
};
const newObj = { ...obj1, ...obj2 };
console.log(newObj === obj1); //false
console.log(newObj === obj2); //false

The spread operator allows you to quickly merge objects. The disadvantage is that you need to use a new variable to receive the

The third method: Object.assign() (most recommended)

const obj1 = {
  name: "zs",
  age: 13,
};
const obj2 = {
  name: "ls",
  sex: "female",
};
const newObj = Object.assign(obj1, obj2);
console.log(newObj === obj1); //true
console.log(newObj === obj2); //false
console.log(newObj);
// {
// name:'ls',
//age:13,
// sex:'female'
// }

The Object.assign() method can receive a target object and one or more source objects as parameters. If the objects have the same properties, the properties of the latter object will overwrite the properties of the former object.

The principle is to add the subsequent objects to the target object through the set access attribute, so the final returned value is actually the first target object. You can add access attributes to the target object to see the overwriting process.

const obj1 = {
  set a(val) {
    console.log(val);
  },
};
Object.assign(obj1, { a: "tom" }, { a: "jerry" }, { a: "dog" });
//'tom'
//'jerry'
//'dog'

This method can be used in many scenarios, and is particularly useful, for example:

1.vue project clear form

Some students may clear the form by assigning empty values ​​to the data in the form one by one. In fact, the efficiency is very low. However, if Object.assign() and $options are used together, the efficiency will be very high.

// Daily this.ruleForm.name='';
this.ruleForm.phone='';
this.ruleForm.imgUrl='';
this.ruleForm.des='';
...10,000 words omitted here // Using Object.assign and $options
Object.assign(this.ruleForm,this.$options.data)

Tips : $options stores the initial value of the Vue instance, so through the Object.assign() overwrite value feature, you can quickly reset the form. Similarly, if you are modifying the form data, you can also quickly assign the ruleForm of the page.

const { data } = await xxxApi.getList();
Object.assign(this.ruleForm, data);

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • js merges multiple objects into one object assign method implementation
  • How to merge two array objects in JavaScript
  • Javascript object merging operation example analysis
  • Detailed explanation of array merging method and object merging method in JavaScript
  • An example of how to merge two Json objects in JavaScript

<<:  Detailed tutorial on building Gitlab server on CentOS8.1

>>:  MySQL learning notes: complete select statement usage example detailed explanation

Recommend

How to enable JMX monitoring through Tomcat

Build a simulation environment: Operating system:...

Deleting files with spaces in Linux (not directories)

In our daily work, we often come into contact wit...

Using jQuery to implement the carousel effect

This article shares the specific code for impleme...

Why should MySQL fields use NOT NULL?

I recently joined a new company and found some mi...

Teach you how to install mysql database on Mac

Download MySQL for Mac: https://downloads.mysql.c...

Two examples of using icons in Vue3

Table of contents 1. Use SVG 2. Use fontAwesome 3...

How to use the dig/nslookup command to view DNS resolution steps

dig - DNS lookup utility When a domain name acces...

MySQL 5.6.36 Windows x64 version installation tutorial detailed

1. Target environment Windows 7 64-bit 2. Materia...

Detailed explanation of destructuring assignment syntax in Javascript

Preface The "destructuring assignment syntax...

XHTML introductory tutorial: Application of table tags

<br />Table is an awkward tag in XHTML, so y...

How to enable remote access permissions in MYSQL

1. Log in to MySQL database mysql -u root -p View...