Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8137767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:15:58+00:00 2026-06-06T11:15:58+00:00

I have following 2 dimensional array input: var arr = [ [‘A’, [‘Sun’,’Moon’]], [‘B’,

  • 0

I have following 2 dimensional array input:

 var arr = [ 
        ['A', ['Sun','Moon']],
        ['B', ['Cat','Dog']],
        ['C', ['John','Peter','Zora']]
 ];

Using that input, I want all combinations applied to a given pattern in javascript:

The placeholders in the pattern are in following format: $(name)

Here are some sample patterns with result:

 var pattern1 = "$(A) / $(A)";
 /* Expected Result
    "Sun / Sun"
    "Sun / Moon"
    "Moon / Sun"
    "Moon / Moon"
 */

 var pattern2 = "$(A)--$(B)";
/* Expected Result
    "Sun--Cat"
    "Sun--Dog"
    "Moon--Cat"
    "Moon--Dog"
*/

 var pattern3 = "$(C) + $(B)";
/* Expected Result
    "John + Cat"
    "John + Dog"
    "Peter + Cat"
    "Peter + Dog"
    "Zora + Cat"
    "Zora + Dog"    
*/

  var pattern4 = "$(A) - $(A) * ( $(B) + $(C) )";
/* Expected Result
    "Sun -  Sun * ( Cat + John )"
    "Sun -  Sun * ( Cat + Peter )"
    "Sun -  Sun * ( Cat + Zora )"
    "Sun -  Sun * ( Dog + John )"
    "Sun -  Sun * ( Dog + Peter )"
    "Sun -  Sun * ( Dog + Zora )"
    "Sun -  Moon * ( Cat + John )"
    "Sun -  Moon * ( Cat + Peter )"
    "Sun -  Moon * ( Cat + Zora )"
    "Sun -  Moon * ( Dog + John )"
    "Sun -  Moon * ( Dog + Peter )"
    "Sun -  Moon * ( Dog + Zora )"
    "Moon -  Sun * ( Cat + John )"
    "Moon -  Sun * ( Cat + Peter )"
    "Moon -  Sun * ( Cat + Zora )"
    "Moon -  Sun * ( Dog + John )"
    "Moon -  Sun * ( Dog + Peter )"
    "Moon -  Sun * ( Dog + Zora )"
    "Moon -  Moon * ( Cat + John )"
    "Moon -  Moon * ( Cat + Peter )"
    "Moon -  Moon * ( Cat + Zora )"
    "Moon -  Moon * ( Dog + John )"
    "Moon -  Moon * ( Dog + Peter )"
    "Moon -  Moon * ( Dog + Zora )"
*/

The pattern can be any combination (with repeating placeholders) and any length.

Can someone help me with an algorithm in javascript?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:15:59+00:00Added an answer on June 6, 2026 at 11:15 am

    You can use a recursive function to do this.

    First you have to extract the names from the string. You can do this with a regular expression:

    var regex = /\$\(([A-Z])\)/g,
        names = [], match;
    
    while(match = regex.exec(pattern)) {
        names.push(match[1]);
    }
    

    Then you pass this list plus a mapping of your values to a function which iterates over the names recursively:

    function create_combinations(pattern, names, map, index) {
        index = index || 0;
        var name = names[index],
            values = map[name],
            needle = "$(" + name + ")",
            combinations = [],
            sub;
    
        if (index === names.length - 1) {
            for (var i = 0, l = values.length; i < l; i++) {
                combinations.push(pattern.replace(needle, values[i]));
            }
        }
        else {
            for (var i = 0, l = values.length; i < l; i++) {
                sub = pattern.replace(needle, values[i]);
                combinations = combinations.concat(create_combinations(sub, names, map, index + 1));
            }
        }
        return combinations;
    }
    

    This requires that your data structure will be an object:

    var map = { 
        'A': ['Sun','Moon'],
        'B': ['Cat','Dog'],
        'C': ['John','Peter','Zora']
    };
    

    Then you call the function with

    create_combinations(pattern, names, map);
    

    DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two dimensional array: static int[,] arr = new int[5, 5]
I have the following three arrays and need to create a new two-dimensional array
I have a two dimensional array with the following output Array ( [0] =>
I have a two-dimensional array. When I print/dump this I get the following My
Simplified, I have to solve the following problem: You have a 2-dimensional array filled
Let's say I have the following empty two dimensional array in Python: q =
Say you have the following ANSI C code that initializes a multi-dimensional array :
I have a multi-dimensional array in the following format: [0] = ( 'id' =>
I have the following two-dimensional array: 01 03 02 15 05 04 06 10
I have the following two dimensional array in my java program. Integer[][] arrayOfSets =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.