Combination of all possible values from first array?

print all possible combinations of r elements in a given array of size n
generate all possible combinations of a set of characters
javascript combinations of multiple arrays
java list all possible combinations of an array
generate all possible combinations of a set of characters java
javascript get all possible combinations of array
find all possible combinations of numbers in java
combinations from n arrays picking one element from each array

How do a combine the array of arrays based on the first array1 or basically group by array1.

Below is the four Array, where i have to form objects based on A and then based on B.

var array1=["A","B"];
var array2=["1","2","3", "4"];
var array3=["N","O","P", "Q"];
var array4=["R"];

Below is how i need :

[ {
  'take': 'A', 
  'take2': '1',
  'take3': 'N',
  'take4': 'R'
}, {
  'take': 'A', 
  'take2': '2',
  'take3': 'N',
  'take4': 'R'
}, {
  'take': 'A', 
  'take2': '3',
  'take3': 'N',
  'take4': 'R'
}, {
  'take': 'A', 
  'take2': '4',
  'take3': 'N',
  'take4': 'R'
}, {
  'take': 'A', 
  'take2': '1',
  'take3': 'O',
  'take4': 'R'
}]

This is something i have tried, but not sure how can i loop n number of n arrays

var result = array1.reduce( (a, v) =>
   [...a, ...array2.map(x=>v+x)],
[]);

here is a keep it simple solution (if you know how many arrays do you have) :

const possibilities = [];

const ar1length = array1.length;
const ar2length = array2.length;
const ar3length = array3.length;
const ar4length = array4.length;

// Not cleanest solution available but it does the job

for ( let i = 0; i < ar1length; i++) {

    for (let j = 0; j < ar2length; j++) {

        for (let k = 0; k < ar3length; k++) {

            for (let l = 0; l < ar4length; l++) {

                possibilities.push({
                    "take": array1[i],
                    "take1": array2[j],
                    "take2": array3[k],
                    "take3": array4[l]
                });


            }


        }

    }
}

Oh and if you want an unknown number of arrays, you may add all of these arrays to an array in order to iterate over it I guess

Print all possible combinations of r elements in a given array of size n, The idea is to start from first index (index = 0) in data[], one by one fix elements at this index and recur for remaining indexes. Let the input array� In this blog, we will learn, how to get all the combination of the elements in an array.Suppose, we have an integer array "myarrint", as given below. int[] myarrint = new[] { 1, 2, 3 }; We need to get all the combination of elements in an array without repeating it. This will be 1,2,3,12,13,21,23,31,32,123,132,213,231,312,321.

I've written a function for this task a while ago, takes an arbitrary amount of Arrays and non-arrays and computes all possible combinations

var array1 = ["A", "B"];
var array2 = ["1", "2", "3", "4"];
var array3 = ["N", "O", "P", "Q"];
var array4 = ["R"];

console.log(combinations(array1, array2, array3, array4).join("\n"));

function combinations(...columns) {
  const state = [], combinations = [state];
  let head = null;
  for (let column = 0; column < columns.length; ++column) {
    let value = columns[column];
    if (Array.isArray(value)) {
      if (value.length > 1) {
        head = {
          next: head,
          column,
          row: 0
        };
      }
      value = value[0];
    }
    state[column] = value;
  }

  let todo = head;
  while(todo) {
    if (++todo.row === columns[todo.column].length) {
      todo.row = 0;
      state[todo.column] = columns[todo.column][todo.row];
      todo = todo.next;
    } else {
      state[todo.column] = columns[todo.column][todo.row];
      combinations.push(state.slice());
      todo = head;
    }
  }

  return combinations;
}
.as-console-wrapper{top:0;max-height:100%!important}

Find all the combinations of the array values in JavaScript , Reduce the first array element by .reduce() method and return the result returned from the recursion result(Recursion is called every time after� For example, if k=3 then one possible combination is {'A','B','C'}. Here we have three different algorithms for finding k-combinations of an array. Forward-Backward Algorithm. Here we have two arrays and two main indices r & i: Array e which is the elements array. Array pointers which is an array for holding indices for selected element. Index

Here's a recursive approach which works for every number of arrays, you just have to call combine(array1, array2, ..., arrayn):

var array1=["A","B"];
var array2=["1","2","3", "4"];
var array3=["N","O","P", "Q"];
var array4=["R"];

function combine(arr1, ...arr2) {
  if(arr2.length === 0) return Array.from(arr1, (x) => x.reduce((obj, y, i) => (obj[`take${i}`] = y, obj), {}));
  
  return combine(arr1.flatMap(d => arr2[0].map(v => {
  return [...Object.values(d), ...Object.values(v)]
  })), ...arr2.slice(1));
}

console.log(combine(array1, array2, array3, array4));

4.24. Finding All Element Combinations of an Array, First, you include the empty set, {} , in the results. After all, one potential combination of a set is to take no elements from it. The rest of this function relies on the� Given an array arr[] of size N, the task is to generate and print all possible combinations of R elements in array.. Examples: Input: arr[] = {0, 1, 2, 3}, R = 3

Generating all combinations of an array, 0 if you start with i=1; If you call the function permutations , then you should not call the list you I've output an array of strings with my combinations function. const powerset = (array) => { // O(2^n) const results = [[]]; for (const value of array) { const copy Yes the 8 4 2 1 method. if bit length is 3 then possible numbers are � array = [1, 2, 3, 4] function permutation(start, end): if end==start: print array return for i -> (start, end+1): for i -> (start, end+1): permutation(start+1,end) Now, 1234 will be printed as it is the first permutation of the number 1, 2, 3 and 4. Till now, we are able to implement the logic of recursion.

Generate All Possible Combinations in JavaScript Using Combinate , The following represents the possible values for each variable: First, let's get combinate install. just have to pass this to our combinate function, and we'll get an array of all of the possible combinations! let's see it in action. Possible duplicate of Algorithm to return all combinations of k elements from n – Random Davis Dec 1 '16 at 21:09 I did read this, it's not combinations it's permutations, it doesn't accept duplicate k values in N, in my case I need all possible combinations even those with duplicate value like (0|0|1 - 2|1|2 ) – devhicham Dec 1 '16 at 21:15

Generate Combinations in Java, Some problems require us to evaluate all possible combinations. In order to do this, we The first approach divides the problem in terms of the elements in the set. The second combinations) {. System.out.println(Arrays. Given an array of size n, find all combinations of size r in the array. Example. INPUT: arr[] = {1,2,3,4} r = 2. OUTPUT: {1,2}, {1,3}, {1,4}, {2,3}, {2,4}, {3,4}

Comments
  • I don't understand the logic to get your desired output
  • @R3tep: Basically i have to form the array of objects based on the first array var array1=["A","B"];
  • you cant have duplicated keys in an object ('take4')
  • Multiple combinations for item A with other array values and similarly multiple combination for item B with other array values.
  • and with for..of for(let take of array1) for(let take1 of array2) for(let take2 of array3) for(let take3 of array4) possibilities.push({ take, take1, take2, take3 });