- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
How to get only first word of object's value – JavaScript?
Let’s say the following is our object −
const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java Angular" } ]
You can use split() on the basis of space.
Example
Following is the code −
const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java Angular" } ] const objectValues = employeeDetails.map(emp => { var [technology1, technology2] = emp.employeeTechnology.split(/\s/); return { technology1, technology2, employeeName: emp.employeeName } }); console.log(objectValues);
To run the above program, use the following command −
node fileName.js.
Here, my file name is demo244.js.
Output
Following is the output on console −
PS C:\Users\Amit\javascript-code> node demo244.js [ { technology1: 'JavaScript', technology2: 'HTML', employeeName: 'John Smith' }, { technology1: 'Java', technology2: 'Angular', employeeName: 'David Miller' } ]
Advertisements