0

I thought this would be easier, but running into a weird issue. I want to split the following:

theList = 'firstword:subwordone;subwordtwo;subwordthree;secondword:subwordone;thirdword:subwordone;subwordtwo;';

and have the output be

firstword
    subwordone
    subwordtwo
    subwordthree
secondword
    subwordone
thirdword
    subwordone
    subwordtwo

The caveat is sometimes the list can be

theList = 'subwordone;subwordtwo;subwordthree;subwordfour;'  

ie no ':' substrings to print out, and that would look like just

    subwordone
    subwordtwo
    subwordthree
    subwordfour

I have tried variations of the following base function, trying recursion, but either get into infinite loops, or undefined output.

function getUl(theList, splitOn){
    var r = '<ul>';
    var items = theList.split(splitOn);

    for(var li in items){
        r += ('<li>'+items[li]+'</li>');
    }
    r += '</ul>';
    return r;
}

The above function is just my starting point and obviously doesnt work, just wanted to show what path I am going down, and to be shown the correct path, if this is totally off base.

4

4 回答 4

1

看来您需要两种情况,两者之间的区别在于:您的字符串中是否有 a 。

if(theList.indexOf(':') == -1){
    //Handle the no sublist case
} else {
    //Handle the sublist case
}

从没有子列表的情况开始,我们开发了简单的模式:

var elements = theList.split(';');
for(var i = 0; i < elements.length; i++){
    var element = elements[i];
    //Add your element to your list
}

最后,我们应用相同的模式来提出子列表案例的实现:

var elements = theList.split(';');
for(var i = 0; i < elements.length; i++){
    var element = elements[i];
    if(element.indexOf(':') == -1){
        //Add your simple element to your list
    } else {
        var innerElements = element.split(':');

        //Add innerElements[0] as your parent element

        //Add innerElements[1] as your child element

        //Increment i until you hit another element with ':', adding the single elements each increment as child elements.

        //Decrement i so it considers the element with the ':' as a parent element.

    }
}
于 2013-02-07T15:39:48.203 回答
0

以下代码段根据您的要求显示列表

   var str  = 'subwordone;subwordtwo;subwordthree;';
    var a = []; var arr = [];
    a = str;
    var final = [];


    function split_string(a){

        var no_colon = true;

        for(var i = 0; i < a.length; i++){

    if(a[i] == ':'){
    no_colon = false;
    var temp;
    var index = a[i-1];
    var rest = a.substring(i+1);
    final[index] = split_string(rest);
    return a.substring(0, i-2);
    }

    } 

    if(no_colon) return a;

    }

    function display_list(element, index, array) {
        $('#results ul').append('<li>'+element+'</li>');
    }


    var no_colon_string = split_string(a).split(';');

    if(no_colon_string){
        $('#results').append('<ul><ul>');
    }
    no_colon_string.forEach(display_list);

    console.log(final);

在这里工作小提琴

于 2013-02-07T18:49:43.370 回答
0

跟踪当前列表以添加项目,并在项目中找到冒号时创建一个新列表:

var baseParent = $('ul'), parent = baseParent;
$.each(theList.split(';'), function(i, e) {
  if (e.length) {
    var p = e.split(':');
    if (p.length > 1) {
      baseParent.append($('<li>').append($('<span>').text(p[0])).append(parent = $('<ul>')));
    }
    parent.append($('<li>').text(p[p.length - 1]));
  }
});

演示:http: //jsfiddle.net/Guffa/eWQpR/

演示:http "1;2;3;4;": //jsfiddle.net/Guffa/eWQpR/2/

于 2013-02-07T15:53:23.847 回答
0

可能有一个更优雅的解决方案,但这可以解决问题。(见下面的编辑

function showLists(text) {
    // Build the lists
    var lists = {'': []};
    for(var i = 0, listKey = ''; i < text.length; i += 2) {
        if(text[i + 1] == ':') {
            listKey = text[i];
            lists[listKey] = [];
        } else {
            lists[listKey].push(text[i]);
        }
    }

    // Show the lists
    for(var listName in lists) {
        if(listName) console.log(listName);

        for(var j in lists[listName]) {
            console.log((listName ? '    ' : '') + lists[listName][j]);
        }
    }
}

编辑 您可以采取的另一种有趣的方法是首先将其分成几个部分(假设text等于您给出的示例之一):

var lists = text.match(/([\w]:)?([\w];)+/g);

然后你把问题分解成更简单的部分

for(var i = 0; i < lists.length; i++) {
    var listParts = lists[i].split(':');
    if(listParts.length == 1) {
        console.log(listParts[0].split(';').join("\n"));
    } else {
        console.log(listParts[0]);
        console.log('    ' + listParts[1].split(';').join("\n    "));
    }
}
于 2013-02-07T15:54:01.510 回答