javascript - How to split a line-by-line string and convert into JSON in Node JS -


i tried split response string.

" hello   how   name   thank " 

in each line surrounded tab spaces infront of starting word , seperated lines. eg: "\t\t\t\t hello \n \t\t\t\t\t how \n......." need convert json object/array like,

{obj: hello, how you, name, thank you} 

i tried won't works.

var $ = cheerio.load(body);   $('div.block-cont:has(div.tplfcol):has(div.song)').each(function( index ) {     var titles = $(this).find('div.song-name > h4').text();     var text = titles.replace("\n", ",");     console.log(str);     var myobj = json.parse('{"obj": "'+text+'"}');     console.log(myobj); }); 

please me

you not need complex regex this. simply, make use of split() , tostring() functions this,

var str  = '\t\t\t\t hello \n \t\t\t\t\t how \n \t\t\t\twhat name \n \t\t thank you';  var res = str.split('\n').map(x=>x.trim()).tostring();  var obj = {obj:res};  console.log(obj);


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -