Warning: session_start(): open(/tmp/sess_790b24efdee9faed002e8f8b7bde30e1, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Rename An Object's Key In JavaScript - LearnShareIT

How To Rename An Object’s Key In JavaScript

How to Rename an Object’s Key in JavaScript

To rename an object’s key in JavaScript, we have tested effectively with four methods: using the assignment, the assign() method, square [] notation, and defineProperty(). Check out the details below to get more information.

Rename an object’s key in JavaScript

Using the assignment

To rename an object’s key, we can easily use an assignment to assign the old object’s key to a new object’s key.

Example:

In this example we will use assignment to assigns old object key: “Class” to new object key: “Major”. Then we delete the old object key: “Class”.

// Create Student with four objects key ID, FullName, Class, YoB
let Student = {
	ID: '17T1020194',
	FullName: 'LearnShareIT',
	Class: 'Information Technology',
	YoB: 2001
};
 
Student.Major = Student.Class;
delete Student.Class;

console.log(Student); 

Output:

ID:"17T1020194"
FullName:"LearnShareIT"
YoB:2001
Major:"Information Technology"

Using assign() method

You can write clean one-line code by using the assign() method in the Javascript program.

Syntax:  

Object.assign(target, ...sources)

Parameters: 

  • target: is an object what to apply the source’s properties, which is returned after modify.
  • sources: is an object containing properties you want to apply.

Return value: return value is a target object.

Example:

In this example, we use the assign() method to copy all properties of the Student and return a new target object. The old object “Class” to be deleted then.

// Create Student with four objects key ID, FullName, Class, YoB
let Student = {
  	ID: '17T1020194',
  	FullName: 'LearnShareIT', 
    Class: 'Infomation Technology',
    YoB: 2001
};
 
delete Object.assign(Student, {Major: Student.Class})['Class'];
console.log(Student); // Object key Class will rename to Major

Output:

ID:"17T1020194"
FullName:"LearnShareIT"
YoB:2001
Major:"Information Technology"

Using the square [] notation

Another simple way to rename an Object’s Key in Javascript is using square[] notation to assign the old key name to the new key name and then use the delete operator to delete the old key. 

Syntax:  

delete object.property
delete object[property]

Parameters: 

  • object: is an object or an expression evaluating an object.
  • property: the property you want to delete.

Return value: return value is true or false.

Example:

In this example, we use square[] notation in javascript to assign the old key “Class” to the new key “Major”. Then delete the old key “Class”.

// Create Student with four objects key ID, FullName, Class, YoB
let Student = {
	ID: '17T1020194',
	FullName: 'LearnShareIT',
	Class: 'Infomation Technology',
	YoB: 2001
};

Student['Major'] = Student['Class'];
delete Student['Class'];
 
console.log(Student); 

Output:

ID:"17T1020194"
FullName:"LearnShareIT"
YoB:2001
Major:"Information Technology"

Using defineProperty

The Object.defineProperty() method defines a new property directly on an object, and the return value is an object.

Syntax:  

Object.defineProperty(object, property, descriptor)

Parameters: 

  • object: is an object to define the property.
  • property: the property to be defined or modified.
  • Descriptor: the descriptor for the property. 

Return value: return value is true or false.

Example:

In this example, we use object.defineproperty() to rename the old object key ‘Class’ to the new object key ‘Major’. After create a new Student, we delete the old object key ‘Class’ in Student then.

// Create Student with four objects key ID, FullName, Class, YoB
let Student = {
	ID: '17T1020194',
	FullName: 'LearnShareIT',
	Class: 'Infomation Technology',
	YoB: 2001
};

Student = Object.defineProperty(Student, 'Major',
	Object.getOwnPropertyDescriptor(Student, 'Class'));
delete Student['Class'];
console.log(Student)

Output:

ID:"17T1020194"
FullName:"LearnShareIT"
YoB:2001
Major:"Information Technology"

Summary

In this tutorial, we have explained how to rename an object’s key in JavaScript by using assignment, assign() method, square [] notation, and Object.defineProperty() method. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article. Thanks for your read.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *