Home » Javascript » Generate all possible combinations of certain string with placeholders and a set of words

Generate all possible combinations of certain string with placeholders and a set of words

Posted by: admin August 19, 2018 Leave a comment

Questions:

I have 3 datasets which are :

patterns=[
    "[foo] is a [bar]",
    "[foo] is not a [bar]",
    "[foo] could be a [bar]"
]

fooSet=[
    "a","b", "c"
]

barSet = [
    "d", "e", "f"
]

these data sets will be dynamic, it could be 5 different patterns and 4 data sets, I have already tried with lodash each and various loops but all I have got is soemthing like this:

“cat in {{country}} for {{cost}}”

“dog in {{country}} for {{cost}}”

“{{animal}} in mex for {{cost}}”

“{{animal}} in usa for {{cost}}”

“{{animal}} in {{country}} for cheap”

“{{animal}} in {{country}} for expensive”

with this data set:

{{animal}} in {{country}} for {{cost}}



cat
dog
iguana
bird

usa
mexico
denmark

cheap
expensive
good price

this is my code:

generateKeywords() {
     //first get an array of patterns
     var patternArray = this.patterns.split(',');
     _this = this;
     newArrayOfPatterns = [];
     _.each(this.elements, function(element) {
         _.each(patternArray, function(pattern) {
             subset = element.list_of_variations.split('\n');
             _.each(subset, function(val) {
                 result = pattern.replace(element.element_caught, val);
                 newArrayOfPatterns.push(result);
             })
         });
 })
}

where elements is an object conatining the words and patterns is the array of patterns

Answers: