 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Assigning values to a computed property in JavaScript?
Following is our object −
const customerDetails=[
   {customerFirstName: "David"},
   {customerLastName: "Miller"},
   {customerCountryName: "US"},
   {customerAge: "29"},
   {isMarried: false},
   {customerCollegeName: null}
];
Let’s assign values to a computed property using slice() along with map().
Example
const customerDetails=[
   {customerFirstName: "David"},
   {customerLastName: "Miller"},
   {customerCountryName: "US"},
   {customerAge: "29"},
   {isMarried: false},
   {customerCollegeName: null}
];
const newCustomerDetails =
customerDetails.slice(2,4).concat(customerDetails[5]).map(obj=>({
   propertyKey: Object.keys(obj)[0],
   propertyValue: Object.values(obj)[0]
}));
console.log(newCustomerDetails);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo135.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo135.js
[
   { propertyKey: 'customerCountryName', propertyValue: 'US' },
   { propertyKey: 'customerAge', propertyValue: '29' },
   { propertyKey: 'customerCollegeName', propertyValue: null }
]Advertisements
                    