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?

Squashman
  • 13,649
  • 5
  • 27
  • 36
TulsaNewbie
  • 382
  • 3
  • 15
  • Why not removing spaces only from needed array's values or before using them instead of removing them at once with loop? – Basel Issmail Mar 06 '19 at 19:37
  • 1
    Shorter yes, but better? No, not really. – Jonas Wilms Mar 06 '19 at 19:38
  • 1
    why not use `trim()` – epascarello Mar 06 '19 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) – r3wt Mar 06 '19 at 19:39
  • 1
    @r3wt that a reversed while loop is faster is a myth until you prove it. – Jonas Wilms Mar 06 '19 at 19:40
  • @JonasWilms https://stackoverflow.com/questions/3520688/javascript-loop-performance-why-is-to-decrement-the-iterator-toward-0-faster-t you don't know pain until you've had to work around a slow for loop. i guess its a non issue in modern js engines which optimize the calls to `array.length` – r3wt Mar 06 '19 at 19:46
  • 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`.) – T.J. Crowder Mar 06 '19 at 19:46
  • @T.J.Crowder yeah you're right, not sure why i thought we had `lTrim()` – r3wt Mar 06 '19 at 19:47

1 Answers1

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.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    you'd have to go back quite some time to require a polyfil for trim, I'd recommend not including one due to bloat. – Steven Stark Mar 06 '19 at 19:40
  • 1
    @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. – T.J. Crowder Mar 06 '19 at 19:44
  • 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! – TulsaNewbie Mar 06 '19 at 19:44