Maintain Array of Objects when using product method

Major Major

I have several array of objects that I need to use product on to get all possible combinations.

When I do the following, it works fine-

combinations = a1.product(a2,a3)
#combinations.class = Array
#combinations[0].class = Object

But if I try to add an array later using the same method, it will turn it into an array of arrays of objects-

combinations = combinations.product(a4)
#combinations.class = Array
#combinations[0].class = Array
#combinations[0][0].class = object

What do I need to change to maintain and array of objects?

Cary Swoveland

Suppose you have

a = [:a1, :a2]
b = [:b1, :b1]
c = [:c1, :c2]

e = a.product(b,c)
  #=> [[:a1, :b1, :c1], [:a1, :b1, :c2], [:a1, :b1, :c1], [:a1, :b1, :c2],
  #    [:a2, :b1, :c1], [:a2, :b1, :c2], [:a2, :b1, :c1], [:a2, :b1, :c2]]

When you take the product of e with:

d = [:d1, :d2]

you get:

f = e.product(d)
  #=> [[[:a1, :b1, :c1], :d1], [[:a1, :b1, :c1], :d2], [[:a1, :b1, :c2], :d1],
  #    [[:a1, :b1, :c2], :d2], [[:a1, :b1, :c1], :d1], [[:a1, :b1, :c1], :d2],
  #    [[:a1, :b1, :c2], :d1], [[:a1, :b1, :c2], :d2], [[:a2, :b1, :c1], :d1],
  #    [[:a2, :b1, :c1], :d2], [[:a2, :b1, :c2], :d1], [[:a2, :b1, :c2], :d2],
  #    [[:a2, :b1, :c1], :d1], [[:a2, :b1, :c1], :d2], [[:a2, :b1, :c2], :d1],
  #    [[:a2, :b1, :c2], :d2]]

but what you want is:

a.product(b,c,d)
  #=> [[:a1, :b1, :c1, :d1], [:a1, :b1, :c1, :d2], [:a1, :b1, :c2, :d1],
  #    [:a1, :b1, :c2, :d2], [:a1, :b1, :c1, :d1], [:a1, :b1, :c1, :d2],
  #    [:a1, :b1, :c2, :d1], [:a1, :b1, :c2, :d2], [:a2, :b1, :c1, :d1], 
  #    [:a2, :b1, :c1, :d2], [:a2, :b1, :c2, :d1], [:a2, :b1, :c2, :d2],
  #    [:a2, :b1, :c1, :d1], [:a2, :b1, :c1, :d2], [:a2, :b1, :c2, :d1],
  #    [:a2, :b1, :c2, :d2]]

You can obtain the result you want by mapping each element of g of f to g.flatten, as @DavidUnric suggested::

e.product(d).map(&:flatten)

or you could do it like this:

e.product(d).map { |a,f| a + [f] }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ruby Array#product method behaviour when given a block

Node JS Inserting array of objects to mysql database when using transactions

Cartesian product of array of objects in javascript

Maintain SQL operator precedence when constructing Q objects in Django

Calling method of objects in array using map()

Trying to maintain index when using gather

Generation cartesian product objects from array values

Does JavaScript maintain a reference to existing variables when objects are created using object literal syntax?

Filtering away javascript objects into an array when using fetch

find if product exists and if so, check if user exists in product array of objects

Print/log objects from an array using a for loop method

How do I call abstract class' child class method when using an array of child objects?

Array of objects on Vue method

In Typescript, how do I maintain an objects type when storing and then accessing said object from an array?

Filter an array of objects using the filter method with multiple tests

No Method Error when using .sum to an array

How to deal with itertools.product when using numpy array?

Editing a method with an array of objects

swap some values in an array of objects while maintain sorted others

Using an object clone method and a for loop to push objects into array, the objects are all still the same reference and are the same in the array

How to maintain Transform of objects when importing from Blender to Unity

Java how to maintain sequence of scanned objects in an arraylist when values are updated

No Method Error when using .sum to an array in Ruby

JavaScript Array not updating when using it in callback method

When using the useEffect hook, the method of using a dependency array is confusing

Mutate an Array of Objects Using the Reduce Method and Spread Operator in JavaScript

Join multiple objects in an array using Jquery method

Add product ratings to an Array of objects JavaScript

Is there a way to maintain C++ array formatting when using VScode's default formatter upon saving?

TOP Ranking

HotTag

Archive