0

I have an array of small strings, but I received that data from outside the program, so I have no idea how the data is formatted. Most specifically, I encounter a lot of situations where I have extraneous white space before and after each string.

Problem is, I have a big array. While I could do something like this:

for (var z = 0; z < myArray.length; z++) {
  myArray[z] = myArray[z].replace(/(^\s+|\s+$)/g,'');
}

or

myArray.forEach(function(part, index) {
  this[index] = this[index].replace(/(^\s+|\s+$)/g,'');
}, myArray);

I'm wondering what would be a better way, or are these pretty much the best? Is there a batch function to do that?

8
  • 1
    Shorter yes, but better? No, not really. Commented Mar 6, 2019 at 19:38
  • 1
    why not use trim() Commented Mar 6, 2019 at 19:38
  • 1
    you could use trim() and lTrim() for trimming white space from start and end of string only, otherwise stick to your regex if you need to trim inside the string as well. in terms of optimization, the fastest you'll get is with a while loop(slightly faster than a for loop) working backwards with a decrementing counter until 0, otherwise you can also use map instead of foreach. either way though, if you have something working, stick with it, if it becomes a problem for performance on the main thread, offload it to a worker process (whether in node.js or in browser, you have options) Commented Mar 6, 2019 at 19:39
  • 1
    @r3wt that a reversed while loop is faster is a myth until you prove it. Commented Mar 6, 2019 at 19:40
  • 1
    @r3wt - trim does both sides, in JavaScript it's trimStart and trimEnd (there's also the older non-standard trimLeft and trimRight which are officially aliases in JavaScript engines on web browsers). (JS doesn't have lTrim.) Commented Mar 6, 2019 at 19:46

1 Answer 1

2

Two suggestions:

  • Rather than replace, on any even vaguely-modern browser I'd use trim, which removes whitespace from the beginning and end of the string. trim was added in ES5 (2009).
  • Unless the array is in the millions of entries, I'd probably use map, but your for loop is just fine. map creates a new array based on the result of a callback function.

Here's both suggestions combined:

myArray = myArray.map(str => str.trim());
// or ES5:
myArray = myArray.map(function(str) { return str.trim(); });

But if you don't want to create a new array, your for with trim is just fine, though you could cache myArray.length to avoid re-retrieving it on every iteration:

for (var i = 0, len = myArray.length; i < len; ++i) {
    myArray[i] = myArray[i].trim();
}

(I'd probably use let instead of var if you're targeting modern environments, so i and len are local to the loop. Modern engines optimize that well now, where they didn't always early on.)

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

3 Comments

you'd have to go back quite some time to require a polyfil for trim, I'd recommend not including one due to bloat.
@StevenStark - I wouldn't worry about the bloat of trim, but you're right about how far back you'd have to go! I misremembered when it was added (I was thinking ES2015, not ES5). Removed all mention of polyfilling. Only very niche software needs to support IE < 11 now.
I had problems with trim actually picking up the whitespace... was very annoying, and I have no earthly idea why it was happening. I looked at the individual characters' ascii and they were all 32. I'll keep working at it. Thanks!

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.