Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
Concatenate Object values with javascript
const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

Im trying to concatenate firstName and lastName to get another object.

const person = {Name:"John Doe", age:50, eyeColor:"blue"};

How can i do that in javascript

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
const { firstName, lastName, ...restOfPerson } = person;
const newPerson = { Name: `${firstName} ${lastName}`, ...restOfPerson };
console.log(newPerson);

I think you are looking for this, you can destructure the object and get the firstName and lastName and use object literal or concatenation to create your Name value.

about 4 years ago · Juan Pablo Isaza Report

0

You can use a getter to get name

Why should we use getter? You can imagine this situation, if you assign Name property, as usual, it will generate setter and getter on that, but seemingly, you're expecting to get value only.

You can check the problem I mentioned with getter and setter below (the code snippet is hidden)

let person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
};

person = {
  ...person,
  Name: person.firstName + " " + person.lastName
};


console.log(person.Name) // John Doe
person.Name = "new name" //assign a new value
console.log(person.Name) //it becomes `new name`!

Here is how we use getter instead of assigning values

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue",
  get Name() {
    return this.firstName + " " + this.lastName
  }
};


console.log(person.Name); //John Doe
person.Name = "new name"; //try to assign a new name
console.log(person.Name); //it's still `John Doe`!

You also can add another function as a class to initialize Name property without modifying the original object (person)

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
};

function PersonWithFullName(data) {
  return {
    ...data,
    get Name() {
      return this.firstName + " " + this.lastName
    }
  }
}

const updatedPerson = new PersonWithFullName(person)

console.log({ person })
console.log({ updatedPerson })

about 4 years ago · Juan Pablo Isaza Report

0

You could use a person factory class:

const p1 = {
  firstName: 'John',
  lastName: 'Doe',
  age: 50,
  eyeColor: 'blue',
};

class Person {
  constructor(person) {
    this.name = `${person.firstName} ${person.lastName}`;
    this.age = person.age;
    this.eyeColor = person.eyeColor;
  }
}

const person = new Person(p1);
console.log(person);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!