Ayodele Kayode
Ayodele Kayode

Reputation: 314

Insert a list inside a TextField

i have a list like this

const list = ["joe", "stack overflow", "gitlab", "hello React!!!"]

and i want to put this list in a text field separated by end line "\n"

i tried getting the values from the array like this

 const values =  Array.of(list).join("\n") 

but i get the value separated by comma instead of a end line,is there another way i can do this.

Upvotes: 0

Views: 174

Answers (6)

GI prim
GI prim

Reputation: 46

This does it for me.

const list = ["joe", "stack overflow", "gitlab", "hello React!!!"]
let values = list.join("\n");
console.log(values);


// result

joe
stack overflow
gitlab
hello React!!!

Upvotes: 1

Adam Jenkins
Adam Jenkins

Reputation: 55613

Array.of([1,2,3]) results in [ [1,2,3] ] - an array with a single element at index 0 which happens to be your array.

As others have pointed out, just do [1,2,3].join("\n"). But this answer tells you why.

Docs for Array.of

Upvotes: 3

Daniel Pantalena
Daniel Pantalena

Reputation: 429

Try without 'Array.of'

let list = ["joe", "stack overflow", "gitlab", "hello React!!!"]

const values = list.join("\n") 

console.log(values);

Upvotes: 1

sonkatamas
sonkatamas

Reputation: 621

list.join("\n")

did the job for me!

Upvotes: 2

Yatrix
Yatrix

Reputation: 13775

["joe", "stack overflow", "gitlab", "hello React!!!"].join("\n") works for me.

Upvotes: 2

Jorropo
Jorropo

Reputation: 358

Just do :

const values = list.join("\n")

Upvotes: 2

Related Questions