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.
content.trim();by itself is just an expression with no change because its not being assigned to anything.letcreates a scope bound instance. So assigning on that is futile for the other scopetrimdoesn'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() )content = content.trim()just reassigns the local parametercontentwith the trimmed string. It will not update the original array.