1

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',
  },
];
3
  • 3
    What is your question? Do you mean 'Result in Terminal' is the result you would like to achieve? Commented Dec 10, 2021 at 20:10
  • I am not the best at explaining, but if you look at the console.logged result, its essentially what I want Commented Dec 10, 2021 at 20:15
  • You're actually quite close, but review if this line makes sense: array.push({ name: 'a ' + values1[i]['name'] }); Commented Dec 10, 2021 at 20:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This mutates the array in place, which it looks like the OP was trying to avoid.
I do mention that ('modified the existing array'), but it wasn't mentioned in the post, so I assumed it was just because they didn't see how to mutate it in place. If that's a requirement it's a fairly easy change. edit: I think your answer is overall better due to the explanation, but I wanted to provide one as well to help OP see where they went wrong.
1

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]);
}

2 Comments

I am trying to avoid ...rest as this code is used at a bigger scale. I just asked this question for a simple reasons
The second example doesn't use ...rest but it does clone the objects using spread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.