Convert Array to String

Hello, I have some problems to convert Array to String. So in my example code i have an array of 4 items. I do know that i can just arr.join(" ") to convert it to a string. However, i don't want to include the first item from the array in my new string. I have created a code that kinda works but is there any better way to do it? This is my example code
let arr = "Item1 Item2 Item3 Item4",
newmsg = ""

arr = arr.split(" ")

if(arr[0] === "Item1") {
for(let i = 1; i < arr.length; i++) {
if(i === arr.length -1) {
newmsg += arr[i]
}else{
newmsg += arr[i]+" "
}
}
console.log(newmsg)
}
let arr = "Item1 Item2 Item3 Item4",
newmsg = ""

arr = arr.split(" ")

if(arr[0] === "Item1") {
for(let i = 1; i < arr.length; i++) {
if(i === arr.length -1) {
newmsg += arr[i]
}else{
newmsg += arr[i]+" "
}
}
console.log(newmsg)
}
10 Replies
Kevin Powell
Kevin Powell•3y ago
Right now, you don't have an array, you only have a string... You need to wrap it in square brackets to make an array, and then you'll want each item be have quotes around it too, since each one is a string, or else it'll just be an array with one item in it let arr = ["Item1", "Item2", "Item3", "Item4"]
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)OP•3y ago
I am using arr.split(" ") to convert the string to an array Then i want to convert it back to a string but not include "Item1 "
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)OP•3y ago
If you check the MDN you can see that it does convert the string to an array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
String.prototype.split() - JavaScript | MDN
The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)OP•3y ago
and also Array.isArray(arr) does return true which confirms that it is indeed an array
Kevin Powell
Kevin Powell•3y ago
oh, fair enough, lol. I just saw the first line and stopped looking, my bad 😅
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)OP•3y ago
Aaah heh :p it's fine 😂
MarkBoots
MarkBoots•3y ago
arr.slice(1) will make an array starting from index 1, then it joins with a space
const newmsg = arr.slice(1).join(" ")
const newmsg = arr.slice(1).join(" ")
if you want to drop the first word from the text, without splitting it to an array, you can do
const string = "Item1 Item2 Item3 Item4"
const newmsg = string.substr(string.indexOf(' ') + 1);
console.log(newmsg) //"Item2 Item3 Item4"//
const string = "Item1 Item2 Item3 Item4"
const newmsg = string.substr(string.indexOf(' ') + 1);
console.log(newmsg) //"Item2 Item3 Item4"//
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)OP•3y ago
Oh thanks. This seems to work !
MarkBoots
MarkBoots•3y ago
with your original loop approach you can simplify it to
let msg = "";
for(let i = 1; i < arr.length; i ++) { //from index 1 to last index off arr (skip index 0)
msg += arr[i] //add arr[index] to msg
if(i !== arr.length - 1) msg += " " //if not the last index, add a space
}
console.log(msg)
let msg = "";
for(let i = 1; i < arr.length; i ++) { //from index 1 to last index off arr (skip index 0)
msg += arr[i] //add arr[index] to msg
if(i !== arr.length - 1) msg += " " //if not the last index, add a space
}
console.log(msg)
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)OP•3y ago
Oh alright. Thank you very much, i will try this 🙂
Want results from more Discord servers?
Add your server