1. Add attributesWhen you copy an object, you add new properties to it. The example copies the user object to userWithPass and adds the password property. const user = { id: 110, name: 'Kayson Li'} const userWithPass = { ...user, password: 'Password!' } user //=> { id: 110, name: 'Kayson Li'} userWithPass //=> { id: 110, name: 'Kayson Li', password: 'Password!' } 2. Merge multiple objectsMultiple objects can be combined into a new object using ... Merge part1 and part2 into user1: const part1 = { id: 110, name: 'Kayson Li' } const part2 = { id: 110, password: 'Password!' } const user1 = { ...part1, ...part2 } //=> { id: 110, name: 'Kayson Li', password: 'Password!' } 3. Remove object propertiesYou can use destructuring in conjunction with the rest operator to remove properties from an object. In this example, password is deconstructed and other attributes are retained in the rest object and returned. const noPassword = ({ password, ...rest }) => rest const user = { id: 110, name: 'Kayson Li', password: 'Password!' } noPassword(user) //=> { id: 110, name: 'Kayson Li' } 4. Dynamically remove attributesLet’s look at an example. The removeProperty function accepts a parameter prop. Using the dynamic property name feature, prop can be dynamically removed from the copied object. const user1 = { id: 110, name: 'Kayson Li', password: 'Password!' } const removeProperty = prop => ({ [prop]: _, ...rest }) => rest // ---- ------ // \ / // Dynamic deconstruction const removePassword = removeProperty('password') const removeId = removeProperty('id') removePassword(user1) //=> { id: 110, name: 'Kayson Li' } removeId(user1) //=> { name: 'Kayson Li', password: 'Password!' } 5. Adjust the order of attributesSometimes the properties of an object are not in the order we want. Using some tricks, you can move attributes to the front or back. To move the id to the front, you can put id: undefined before the expanded object: const user3 = { password: 'Password!', name: 'Bruce', id: 300 } const organize = object => ({ id: undefined, ...object }) // ------------- // / // Move id to the first attribute position organize(user3) //=> { id: 300, password: 'Password!', name: 'Bruce' } To move password to the last position, first deconstruct password from object, and then put password after the expanded object: const user3 = { password: 'Password!', name: 'Bruce', id: 300 } const organize = ({ password, ...object }) => ({ ...object, password }) //-------- // / // Move password to the end organize(user3) //=> { name: 'Bruce', id: 300, password: 'Password!' } 6. Set property default valuesWhen an object does not have a certain attribute, we sometimes need to add this attribute to the object and set a default value. For example, user2 does not have a quotes attribute. The purpose of the setDefaults function is to ensure that all objects have quotes and have a default value []. Execute setDefaults(user2) and the returned object contains quotes: []. Execute setDefaults(user4). Since user4 already has quotes, it remains unchanged. const user2 = { id: 200, name: 'Jack Ma' } const user4 = { id: 400, name: 'Lu Xun', quotes: ["I never said this..."] } const setDefaults = ({ quotes = [], ...object}) => ({ ...object, quotes }) setDefaults(user2) //=> { id: 200, name: 'Jack Ma', quotes: [] } setDefaults(user4) //=> { //=> id: 400, //=> name: 'Lu Xun', //=> quotes: ["I never said this..."] //=> } If you want this attribute to be at the front, you can write it like this: const setDefaults = ({ ...object}) => ({ quotes: [], ...object }) 7: Attribute renamingCombining the previous techniques, we can write a function to rename attributes. Suppose some objects have uppercase ID attributes, and we want to change them to lowercase. What should we do? First, deconstruct the ID value from the object, and then merge this value into the new object and change it to a lowercase id: const renamed = ({ ID, ...object }) => ({ id: ID, ...object }) const user = { ID: 500, name: "Zhang Quandan" } renamed(user) //=> { id: 500, name: '张全蛋' } 8. There are more amazing operationsYou can decide whether to add a certain attribute based on conditions, which is very useful when constructing request parameters. For example, we add this attribute only if password has a value: const user = { id: 110, name: 'Kayson Li' } const password = 'Password!' const userWithPassword = { ...user, id: 100, ...(password && { password }) } userWithPassword //=> { id: 110, name: 'Kayson Li', password: 'Password!' } The above are the details of several wonderful uses of JS ES6 spread operator. For more information about JS ES6 spread operator, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed tutorial for installing mysql5.7.21 under Windows system
>>: Execute initialization sql when docker mysql starts
Dark background style page design is very popular...
1. Command Introduction The ln command is used to...
Preface Usually, a "paging" strategy is...
First, let me show you the finished effect Main i...
This article uses an example to describe how to u...
To perform incremental backup of the MySQL databa...
Table of contents 1. Preliminary preparation 1.1 ...
Now, more and more front-end developers are starti...
There are always some problems when configuring n...
In the previous article [Detailed explanation of ...
The development of Docker technology provides a m...
Table of contents 1. What is an event? 2. How to ...
Introduction to AOP The main function of AOP (Asp...
Table of contents 1. Deploy consul cluster 1. Pre...
Table of contents 1. Recipe Collection 1.1 Projec...