What is the method of manipulating objects in JavaScript
This article is to share with you about the method of operating objects in JavaScript, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Object.create ()
Create a new object instance based on the parameter.
Const user = {name: 'kylin', age: 18, gender:' female', work: 'dev', say () {console.log (`name: ${this.name}, age: ${this.age}, gender: ${this.gender} `);}}; const my = Object.create (user); my.say (); / / name: kylin, age: 18, sex: female my.name =' Tom' My.year = '2020; console.log (my.name); / / Tom console.log (my.year); / / 2020
Object.assign ()
Merge (copy) all enumerable properties from one or more objects to a new object.
The Object.assign () method has two parameters, the first is the target object, and the second is the source object. If there is an attribute of the same name, the property of the later object overrides the property of the previous object.
Const userBasic = {name: 'kylin', age: 20,}; const userInfo = {age: 18, gender:' female', work: 'dev', say () {console.log (`name: ${this.name}, age: ${this.age}, gender: ${this.gender}, job: ${this.work} `);}}; const user = Object.assign (userInfo, userBasic); user.say () / / name: kylin, age: 20, gender: female, job: dev
You can also merge into a new object.
Const userBasic = {name: 'kylin', age: 18, gender:' female', say () {console.log (`name: ${this.name}, age: ${this.age}, gender: ${this.gender} `);}}; const user = Object.assign ({}, userBasic); user.say (); / / name: kylin, age: 18, sex: female
Object.keys ()
Returns an array of all properties that can be enumerated by the object itself.
Const user = {name: 'kylin', age: 18}; Object.defineProperty (user,' gerder', {value: "female", enumerable: false / / not enumerable}); console.log (Object.keys (user)); / / ["name", "age"]
Object.values ()
Returns an array of values of all the enumerable properties of the object itself, in the same order as using the for...in loop.
Const user = {name: 'kylin', age: 18}; Object.defineProperty (user,' gerder', {value: "female", enumerable: false / / non-enumerable}); console.log (Object.values (user)); / / ["kylin", 18]
Object.entries ()
The properties and values (keys and values) of the object are represented as an array. That is, an array of key-value pairs containing the enumerable properties of the object itself is returned.
Const user = {name: 'kylin', age: 18}; const info = Object.entries (user); console.log (info); / / [[' name', 'kylin'], [' age', 18]]
Object.fromEntries ()
This is equivalent to the inverse operation of the Object.entries () method, which converts an array in the form of key-value pairs into objects.
Const user = Object.fromEntries ([['name',' kylin'], ['age', 18]]); console.log (user); / / {name: "kylin", age: 18} these are the methods of manipulating objects in JavaScript. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.