How to get only first word of object's value – JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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'
   }
]
raja
Published on 03-Oct-2020 19:04:13
Advertisements