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

174
Views
Adding text to beginning of Object property value

Hi there I am adding the string 'a ' to the beginning of the value of name. I have also added a condition where if the value of name.length === 3, then add the string 'a ' but it is only returning the objects that get changed and only the name property.

var values1 = [
  {
    name: 'dog',
    surname: 'good',
    skills: 'programming',
  },
  {
    name: 'cat',
    surname: 'soft',
    skills: 'engineer',
  },
  {
    name: 'elephant',
    surname: 'big',
    skills: 'programming',
  },
];

let array = [];
for (let i = 0; i < values1.length; i++) {
  if (values1[i]['name'].length == 3) {
    array.push({ name: 'a ' + values1[i]['name'] });
  }
}

for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

This is the result I would like to return.

[
  {
    name: 'a dog',
    surname: 'good',
    skills: 'programming',
  },
  {
    name: 'a cat',
    surname: 'soft',
    skills: 'engineer',
  },
  {
    name: 'elephant',
    surname: 'big',
    skills: 'programming',
  },
];
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Here is an example using map() and destructuring the name property in the callback.

var values1 = [
  { name: 'dog', surname: 'good', skills: 'programming' },
  { name: 'cat', surname: 'soft', skills: 'engineer' },
  { name: 'elephant', surname: 'big', skills: 'programming' },
];

const result = values1.map(({ name, ...rest }) => 
  ({ name: (name.length === 3 ? `a ${name}` : name), ...rest }));

console.log(result);

But to make your code work you simply need to be sure to push the whole object regardless of whether the name is changed or not. Here creating a copy of the object using Object.assign() updating the name if needed, then pushing the copy to the array.

var values1 = [
  {
    name: 'dog',
    surname: 'good',
    skills: 'programming',
  },
  {
    name: 'cat',
    surname: 'soft',
    skills: 'engineer',
  },
  {
    name: 'elephant',
    surname: 'big',
    skills: 'programming',
  },
];

let array = [];
for (let i = 0; i < values1.length; i++) {
  const obj = Object.assign({}, values1[i]);

  if (obj['name'].length === 3) {
    obj.name = 'a ' + obj['name'];
  }

  array.push(obj);
}

for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

about 4 years ago · Santiago Gelvez Report

0

You're quite close, but instead of adding a name key (push()) you want to reassign the value of the name field.

var values1 = [
  {
    name: 'dog',
    surname: 'good',
    skills: 'programming',
  },
  {
    name: 'cat',
    surname: 'soft',
    skills: 'engineer',
  },
  {
    name: 'elephant',
    surname: 'big',
    skills: 'programming',
  },
];

for (let i = 0; i < values1.length; i++) {
  if (values1[i]['name'].length == 3) {
    values1[i]['name'] = 'a ' + values1[i]['name'];
  }
}

console.log(values1)

I modified the existing array rather than adding a new one. I don't think it's dangerous as we are updating a primitive.

about 4 years ago · Santiago Gelvez 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!