- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 } ]
- Related Articles
- How to Declare an Object with Computed Property Name in JavaScript?
- Assigning Values to Variables in Python
- Assigning function to variable in JavaScript?
- Assigning values to static final variables in java
- How are the exception values computed?
- Assigning long values carefully in java to avoid overflow
- How to remove duplicate property values in array – JavaScript?
- What is the correct way to replace matplotlib tick labels with computed values?
- Group values on same property - JavaScript
- MongoDB query to push a computed expression in a $group?
- Assigning arrays in Java.
- How can we insert values into a table with the help of MySQL self-computed output?
- How to create sequential index value for binary column assigning 0 to FALSE values in R data frame?
- How to create sequential index value for binary column assigning 0 to FALSE values in data.table object in R?
- Sorting an array of objects by property values - JavaScript

Advertisements