1

I have an array like this:

const allData = 
[
  ['Alex   ','foo@email','   ',],
  ['  ','goo@gmail, '232-333-2222'],
]

I want to trim all values within the array so that for example 'Alex ' becomes 'Alex'.

I have tried these solutions:

for (let data of allData.entries()) {
    for (let content of data) {
        content.trim();
    }
}
allData.forEach((data) => {
    data.forEach((content) => {
        content.trim();
    });
});
allData.map((data, index) => {
    return data.map((content) => content.trim());
});

I have tried content = content.trim(); for all of them as well.

But even though it does trim the value it won't save the modified value in the array.

What am I missing? I want allData to contain the trimmed values.

6
  • 1
    content.trim(); by itself is just an expression with no change because its not being assigned to anything. Commented Feb 7, 2021 at 10:27
  • Your code does the trimming but does not store the trimmed value back to the array. Commented Feb 7, 2021 at 10:28
  • let creates a scope bound instance. So assigning on that is futile for the other scope Commented Feb 7, 2021 at 10:31
  • 1
    trim doesn't modify the original string as they are immutable. You need to replace the array's index by changing the inner forEach to: data.forEach((content, i) => data[i] = content.trim() ) Commented Feb 7, 2021 at 10:33
  • 1
    And content = content.trim() just reassigns the local parameter content with the trimmed string. It will not update the original array. Commented Feb 7, 2021 at 10:42

2 Answers 2

2

There are no issues with your code (the third version). The only misunderstanding is that .map() does not modify the original array, but return a new one. Look:

// This is basically your code.
const allData = 
[
  ['Alex   ','foo@email','   ',],
  ['  ','goo@gmail', '232-333-2222'],
];

 const trimmedAllData = allData.map((data, index) => {
    return data.map((content) => content.trim());
 });
 
 console.log(trimmedAllData);

Your second try can be easily fixed to modify the original allData values:

const allData = 
[
  ['Alex   ','foo@email','   '],
  ['  ','goo@gmail', '232-333-2222'],
];

allData.forEach((data, i) => {
    data.forEach((content, j) => {
        allData[i][j] = content.trim(); // You have to write the trimmed value back!
    });
});
 
console.log(allData);

The first version is not a good enough approach.

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

5 Comments

This has nothing to with map not modifying the original array. Even forEach isn't working for OP
@adiga: I think it does. The OP has a basic misconception there as you can clearly see looking at his/her first and second solution. For the third one I think it is basically the same problem. So while this provides a solution I am not sure that it entirely removes the misconception the OP has.
@KrisztiánBalla the answer doesn't explain why content.trim() doesn't work in forEach or why content = content.trim() doesn't work.
@adiga I thought that is just what I've said. :-)
Thanks everyone. I have extended my answer a bit in order to make it more understandable.
1

You can use two nested .map and save the result in a new variable res:

const allData = 
[
  ['Alex   ','foo@email','   ',],
  ['  ','goo@gmail', '232-333-2222'],
];

const res = allData.map( arr => arr.map(e => e.trim()) );

console.log(res);

If you want to use the for loops, you need to directly change the elements:

const allData = 
[
  ['Alex   ','foo@email','   ',],
  ['  ','goo@gmail', '232-333-2222'],
];

allData.forEach((data,i) => {
  data.forEach((content,j) => {
    allData[i][j] = content.trim();
  });
});

console.log(allData);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.