Dùng split(',') tách chuỗi, sau đó map(trim()) xóa khoảng trắng 2 đầu.
Mã thực hiện
var sentences = " John , David , Bob , Mike, Carol ";
var result = sentences.split(",").map(v => v.trim());
console.log("After modifying the value=");
console.log(result);
Kết quả
[ 'John', 'David', 'Bob', 'Mike', 'Carol' ]