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

When working with objects that contain string values with multiple words, you often need to extract only the first word. The most effective approach is using the split() method with a space delimiter.

Sample Data

Let's work with an employee object containing names and technologies:

const employeeDetails = [
   {
      employeeName: "John Smith",
      employeeTechnology: "JavaScript HTML"
   },
   {
      employeeName: "David Miller",
      employeeTechnology: "Java Angular"
   }
];

console.log("Original data:", employeeDetails);
Original data: [
   {
      employeeName: 'John Smith',
      employeeTechnology: 'JavaScript HTML'
   },
   {
      employeeName: 'David Miller',
      employeeTechnology: 'Java Angular'
   }
]

Method 1: Extracting First Word Only

To get only the first word from each technology field:

const employeeDetails = [
   {
      employeeName: "John Smith",
      employeeTechnology: "JavaScript HTML"
   },
   {
      employeeName: "David Miller", 
      employeeTechnology: "Java Angular"
   }
];

const firstWordOnly = employeeDetails.map(emp => {
   const firstTech = emp.employeeTechnology.split(' ')[0];
   return {
      employeeName: emp.employeeName,
      primaryTechnology: firstTech
   };
});

console.log(firstWordOnly);
[
   {
      employeeName: 'John Smith',
      primaryTechnology: 'JavaScript'
   },
   {
      employeeName: 'David Miller',
      primaryTechnology: 'Java'
   }
]

Method 2: Splitting Into Multiple Properties

You can also split the technology string and create separate properties for each word:

const employeeDetails = [
   {
      employeeName: "John Smith",
      employeeTechnology: "JavaScript HTML"
   },
   {
      employeeName: "David Miller",
      employeeTechnology: "Java Angular"
   }
];

const splitTechnologies = employeeDetails.map(emp => {
   const [technology1, technology2] = emp.employeeTechnology.split(/\s/);
   return {
      technology1,
      technology2,
      employeeName: emp.employeeName
   };
});

console.log(splitTechnologies);
[
   {
      technology1: 'JavaScript',
      technology2: 'HTML',
      employeeName: 'John Smith'
   },
   {
      technology1: 'Java',
      technology2: 'Angular',
      employeeName: 'David Miller'
   }
]

Method 3: Handling Multiple Words

For strings with more than two words, you can extract the first word and keep the rest:

const employeeDetails = [
   {
      employeeName: "John Smith",
      employeeTechnology: "JavaScript HTML CSS React"
   },
   {
      employeeName: "David Miller",
      employeeTechnology: "Java Angular Spring Boot"
   }
];

const processedData = employeeDetails.map(emp => {
   const techArray = emp.employeeTechnology.split(' ');
   return {
      employeeName: emp.employeeName,
      primaryTech: techArray[0],
      otherTech: techArray.slice(1).join(' ')
   };
});

console.log(processedData);
[
   {
      employeeName: 'John Smith',
      primaryTech: 'JavaScript',
      otherTech: 'HTML CSS React'
   },
   {
      employeeName: 'David Miller',
      primaryTech: 'Java',
      otherTech: 'Angular Spring Boot'
   }
]

Key Points

  • split(' ')[0] extracts only the first word
  • split(/\s/) uses regex to split on any whitespace character
  • Array destructuring [tech1, tech2] assigns split values to variables
  • slice(1) gets all elements except the first one

Conclusion

Use split(' ')[0] to extract the first word from object string values. Combine with map() to process arrays of objects efficiently.

Updated on: 2026-03-15T23:19:00+05:30

462 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements