admin管理员组文章数量:1336729
I want to get all neighboring binations of words in string like
string get all binations
and I want to get
get all binations
all binations
get all
all
get
binations
and I write next code
var string = 'get all binations';
var result = getKeywordsList(string);
document.write(result);
function getKeywordsList(text) {
var wordList = text.split(' ');
var keywordsList = [];
while (wordList.length > 0) {
keywordsList = keywordsList.concat(genKeyWords(wordList));
wordList.shift();
}
return keywordsList;
}
function genKeyWords(wordsList) {
var res = [wordsList.join(' ')];
if (wordsList.length > 1) {
return res.concat(genKeyWords(wordsList.slice(0, -1)));
} else {
return res;
}
}
I want to get all neighboring binations of words in string like
string get all binations
and I want to get
get all binations
all binations
get all
all
get
binations
and I write next code
var string = 'get all binations';
var result = getKeywordsList(string);
document.write(result);
function getKeywordsList(text) {
var wordList = text.split(' ');
var keywordsList = [];
while (wordList.length > 0) {
keywordsList = keywordsList.concat(genKeyWords(wordList));
wordList.shift();
}
return keywordsList;
}
function genKeyWords(wordsList) {
var res = [wordsList.join(' ')];
if (wordsList.length > 1) {
return res.concat(genKeyWords(wordsList.slice(0, -1)));
} else {
return res;
}
}
can I improve or simplify this task (get all neighboring binations of words ) p.s. sorry for my English
Share Improve this question edited Oct 27, 2014 at 11:11 dyachenko 1,21214 silver badges29 bronze badges asked Oct 27, 2014 at 8:24 user4184969user4184969 4- 2 If the code works as intended, it's better asked on Code Review. – Jongware Commented Oct 27, 2014 at 8:36
- @Sergey -- just be careful with recursive functions, they are limited depending the browser you use. In fact wouldnt it be better to just send that list server side to handle it with PhP? – kockburn Commented Oct 27, 2014 at 8:38
- @Grimbode thanks I will be careful with it – user4184969 Commented Oct 27, 2014 at 8:41
- @Jongware i didn't know about it – user4184969 Commented Oct 27, 2014 at 8:42
1 Answer
Reset to default 11hello maybe this help you
var string = 'get all binations'
var sArray = string.split(' ');
var n = sArray .length;
for (var i = 0; i < n; i++) {
for (var j = 0; j <= i; j++) {
document.write(sArray .slice(j, n - i + j).join(' ') + ', ');
}
}
本文标签: javascriptHow to get all combinations of word in stringStack Overflow
版权声明:本文标题:javascript - How to get all combinations of word in string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392376a2466242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论