Javascript 拆分字符串并找到其结果的有序组合的最优雅方法

Javascript 拆分字符串并找到其结果的有序组合的最优雅方法,javascript,underscore.js,Javascript,Underscore.js,我正在寻找最优雅的方法,用分隔符分割字符串,并找到可能的组合 e、 g: 'foo.bar.baz' => ['foo','foo.bar','foo.bar.baz'] 我也不介意使用下划线 编辑: 到目前为止我都试过了 function comb(words) {

我正在寻找最优雅的方法,用分隔符分割字符串,并找到可能的组合

e、 g:

'foo.bar.baz'

=>

['foo','foo.bar','foo.bar.baz']

我也不介意使用下划线

编辑:

到目前为止我都试过了

function comb(words) {                                                                                                                       
    var combinations = [];
    for (var i = 0; i < words.length; i++) {  
        var currentState = [];                                                                                                               
        for (var j = 0; j <= i; j++) {                                                                                                       
            currentState.push(words[j]);                                                                                                     
        }                                                                                                                                    
        console.log('current state', currentState.join('.'));                                                                                
        combinations.push(currentState.join('.'));                                                                                           
    }                                                                                                                                        
    return combinations;                                                                                                                     
}                                                                                                                                            

console.log('combinations', comb('foo.bar.baz'.split('.')));    
函数梳(字){
var组合=[];
对于(var i=0;i对于(var j=0;j我不确定什么是“最优雅的”,但这里有一个使用下划线的相当简洁的解决方案:

function comb(words) {
    var accumulate = "";
    return _.map(words.split("."), function(word) {
        var t = accumulate + word;
        accumulate = t + ".";
        return t;
    });
 }
与您的示例稍有不同,因为
comb
的参数是您要拆分的字符串。显然,您可以在调用之前拆分,就像在您的示例中一样,而不是在函数内部进行拆分。

还有另一种方法:

function comb(words) {
    var combinations = [], temp = "";
    for (var i = 0; i < words.length; i++) {
        combinations.push(temp + words[i]);
        temp += words[i] + ".";
    }
    return combinations;
}

console.log('combinations', comb('foo.bar.baz'.split('.'))); 
function comb(str) {
    var index = -1, arr = [];
    while( (index = str.indexOf('.', index + 1) ) >= 0 ) {
        arr.push(str.substring(0, index));
    }
    arr.push(str);
    return arr;
}

console.log("combinations are: " + comb("foo.bar.baz"));

到目前为止您尝试了什么?
.split()
.match()
?在您的示例中,
bar.baz
不是一个有效的组合吗?还有,
foo.baz
?如何排序?按字母顺序是bar.baz.foo…用我当前的代码和“排序”的东西更新了我的问题。相同的解决方案一行代码:
home.users.list'。拆分('..).reduce(函数(res,word){res arr.push(res.s+=word);res.s+='.';return res;},{arr:[],s:'})。arr;
@plalx Post it。公平地说,它不是一行,因为一行上有多个语句。但它巧妙地使用了对象作为初始值,并且具有不依赖下划线js的优点。
function comb(str) {
    var index = -1, arr = [];
    while( (index = str.indexOf('.', index + 1) ) >= 0 ) {
        arr.push(str.substring(0, index));
    }
    arr.push(str);
    return arr;
}

console.log("combinations are: " + comb("foo.bar.baz"));