Fajny_Kubo
Fajny_Kubo

Reputation: 37

How can I convert one value from DB to array

I saved ONE or MORE THAN ONE values from multiply select option to one value in DB. for example:" Name1, Name2, Name3 "

When I leave from page and then return to page I need load and preselected these values to multiply select option.

I need this :

$("#form_meno").val([**"Name1", "Name2", "Name3"**]);

But I dont know how I convert it.

Upvotes: 1

Views: 52

Answers (1)

Maheer Ali
Maheer Ali

Reputation: 36594

You can use split() after removing the whitespaces using replace()

let str = " Name1, Name2, Name3 ";
let arr = str.replace(/\s+/g,'').split(',');
console.log(arr)

Upvotes: 1

Related Questions