Javascript将字符串转换为多维数组[关闭]

Javascript将字符串转换为多维数组[关闭]

问题描述:

I am outputting from a PHP document my database query in a form of a string, something along the lines of;

ID-ImagePath-TitleOfPost-Text-UpVotes|ID-ImagePath-TitleOfPost-Text-UpVotes|ID-ImagePath-TitleOfPost-Text-UpVotes

I would like to organise this string into a JS array so that I can load the posts one by one. I have tried multiple methods but as yet unable to find a solution. Any help greatly appreciated.

我从一个PHP文档输出我的数据库查询,其形式为字符串,类似于; p>

ID-的ImagePath-TitleOfPost-文本UpVotes | ID-的ImagePath-TitleOfPost-文本UpVotes | ID-的ImagePath-TitleOfPost-文本UpVotes P>

我想将这个字符串组织成一个JS数组,以便我可以逐个加载这些帖子。 我尝试了多种方法但尚无法找到解决方案。 任何帮助都非常感谢。 p> div>

Something like:

var input = "ID-ImagePath-TitleOfPost-Text-UpVotes|ID-ImagePath-TitleOfPost-Text-UpVotes|ID-ImagePath-TitleOfPost-Text-UpVotes";

var output = input.split('|').map(function(x) { return x.split('-'); });

console.dir(output);

I am outputting from a PHP document my database query in a form of a string

If you can affect the outputting string - don't output it like this!

Use json_encode in php and simply JSON.parse in javascript