I have an array that contains multiple strings. I need to store each string minus the first letter and then concatenate them into a sentence.
I am trying:
var missingFirstLetter = array[i].splice(1);
What I have found online guides me to believe this should work, but it doesn't work as intended.
You should slice (not splice!) each element of the array and then store it back into an array, which you can do with Array#map
, which maps each element to a new value, in this case the string without the first letter:
var arrayNoFirstLetter = array.map(el => el.slice(1));
This will iterate through the array and map each element to a new string without the first letter and store the new array of strings into arrayNoFirstLetter
. Make sure to use String#slice
to get a section of a string, because there is not String#splice
method. (maybe you mistook it for Array#splice
?) Then you can use Array#join
to join them with a delimiter (which is the string between each element when joined together):
var joined = arrayNoFirstLetter.join(""); //join with empty space for example
For example:
var array = ["Apples", "Oranges", "Pears"];
var arrayNoFirstLetter = array.map(el => el.slice(1)); // ["pples", "ranges", "ears"]
var joined = arrayNoFirstLetter.join(""); // "pplesrangesears"