JavaScript JavaScript split add an extra empty array item? javascript split extra element

The following code reads a file and turned each line into array items:

fs.readFile('en.txt', 'utf8', function (err, data) {
  if (err) {
    return console.log(err)
  }

  enStrings = data.split(/[\r\n]+/g)
}

en.txt looks like this:

Line 1
Line 2
Line 3

But I'm puzzled. console.log(enStrings) outputs this:

[ 'Line 1', 'Line 2', 'Line 3', '' ]

Why is that last empty item being added? And how to remove it?

Answer:1

This would happen if your text file has a trailing new line character, which is common.

Why not trim before splitting?

enStrings = data.trim().split(/[\r\n]+/g);

Alternately, you could remove just the trailing new line characters before splitting.

enStrings = data.replace(/[\n\r]+$/, '').split(/[\r\n]+/g)

However, if your data is long, you may want to avoid the performance hit of recreating the entire string before splitting. If that is the case, you could use the following to pop it off the end.

if (enStrings.length && !enStrings[enStrings.length-1]) {
    enStrings.pop();
}
Answer:2

You may use filter:

console.log(enStrings.filter(Boolean));

Empty strings are falsy value so using .filter(Boolean) will only list the truthy value and removes the empty strings from your array.

Answer:3

How can I force the browser to automatically reload a rails app at 5am each day? I have a web app that is installed on a wall mounted TV that functions as a sort of live dashboard at our office. It ...

How can I force the browser to automatically reload a rails app at 5am each day? I have a web app that is installed on a wall mounted TV that functions as a sort of live dashboard at our office. It ...

I'm trying to detect when an element gets added to the dom, like so: import {DOM} from 'rx-dom'; let mutationStream = (selector) => { return DOM.fromMutationObserver(getElementsFromSelector(...

I'm trying to detect when an element gets added to the dom, like so: import {DOM} from 'rx-dom'; let mutationStream = (selector) => { return DOM.fromMutationObserver(getElementsFromSelector(...

I'm trying to write a function that will let me update my kitchen in this example below. Using something like underscore's extend, however, blows away the beer in my fridge as it's updating the entire ...

I'm trying to write a function that will let me update my kitchen in this example below. Using something like underscore's extend, however, blows away the beer in my fridge as it's updating the entire ...

  1. merge javascript objects in array with same key
  2. merge javascript objects with same key
  3. merge javascript objects in array with same key lodash
  4. merge javascript objects in array with same value
  5. merge javascript objects
  6. merge javascript objects by key
  7. merge javascript objects in array
  8. merge javascript objects in array with same key es6
  9. merge javascript objects lodash
  10. merge js objects
  11. javascript merge objects deep
  12. javascript merge objects es6
  13. javascript merge objects without overwriting
  14. javascript merge objects spread
  15. javascript merge objects overwrite
  16. javascript merge objects recursive
  17. javascript merge objects based on key
  18. javascript merge objects properties
  19. javascript merge objects es5
  20. javascript merge objects spread operator

Using setInterval or RequestAnimationFrame, I'd like to get the progression value from lerping between X and Y. Assuming that X is 0 and Y is 1, I want to have 0 when it starts, 0.5 in the half, and 1 ...

Using setInterval or RequestAnimationFrame, I'd like to get the progression value from lerping between X and Y. Assuming that X is 0 and Y is 1, I want to have 0 when it starts, 0.5 in the half, and 1 ...

  1. lerp back and forth unity
  2. lerp back and forth
  3. unity3d lerp back and forth