-1

I'm having trouble figuring out how to remove items from an array after using the join() function.

As you'll see, the first array.splice() works, but the second does not...

var recordsArray1 = ['a','b','c','d','e','f'];
 
console.log(recordsArray1.length);
recordsArray1.splice('a', 1);
console.log(recordsArray1);

var recordsArray2 = ['g','h','i','j','k','l'].join('\n');
 
console.log(recordsArray2.length);
recordsArray2.splice('g', 1);
console.log(recordsArray2);

Thanks for any help.

Tim

Tim Samoff
  • 94
  • 11
  • Of course, the best way would be to generate the visual representation via a for loop, applying all formatting there. – Tim Samoff May 26 '17 at 19:33
  • `splice` is being used incorrectly. The first parameter is supposed to be an _index_ at which to start operations. You put a character in there. [Read the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) – qarthandso Aug 22 '18 at 07:08

4 Answers4

1

Array.prototype.join(separator) converts an array into a string seperated by separator. If you want to join the elements with a newline, perform the join after any other array operations, like slicing.

James Kraus
  • 3,349
  • 20
  • 27
  • How would I create a (vertical) list of array items first and then remove items later? – Tim Samoff May 26 '17 at 18:28
  • If you wanted to, you could append a "\n" to every item and then filter them. That could be done with something like list.map(item => item + "\n") – James Kraus May 26 '17 at 18:29
  • 1
    The reality is though, there is not such thing as a "vertical list of array items". Array items are just items, in an array, in memory. I think you're muddling the representation of an array and the array itself. – James Kraus May 26 '17 at 18:37
  • Ok, you are correct. So… In thinking about visual representation of data in an array, I'd like to take the data and present it in a vertical list (no commas, etc.). Then, I'd like to — via a function — remove data from the array and redraw the list with the new data. – Tim Samoff May 26 '17 at 18:50
  • The easiest method would just be to create a list of items and use jQuery to remove list items. But I'd like to think about this more like "data" than arbitrary items in a list. – Tim Samoff May 26 '17 at 18:52
0

https://www.w3schools.com/jsref/jsref_join.asp

join turns it from an array into a string

Alex
  • 419
  • 6
  • 24
0

If I understood your need correctly, you may want to map over the elements and add newline to each, instead of joining the array to a string. This should do:

recordsArray2 = ['g','h','i','j','k','l']
recordsArray2.map((x) => x + "\n")
Alon
  • 57
  • 1
  • 7
0

First, your usage of splice is not right. Your intention seems to be "please, remove exactly one a" respectively "please remove a g". But that's not how splice works. The first parameter is the index to start at, the correct usage would be .splice(0, 1) (read: remove one element starting from index 0).

Second, join converts the Array to a string using the provided sequence. So, ['g','h','i','j','k','l'].join('\n'); yields g\nh\ni\n... as a string, which does not have a join method. So, to remove the g, I suggest to join the array after you removed the g: var arr =['g','h','i','j','k','l']; arr.splice(0, 1); arr = arr.join('\n'); (or do not join it after all, since I think it was not what you intended to do). Another way would to remove the G after joining, e.g. var arr =['g','h','i','j','k','l'].join('\n').substr(2) (which would return the string starting at the third character, so the g and the following \n are removed).

SVSchmidt
  • 6,269
  • 2
  • 26
  • 37