How to realize Cartesian product of array

problem background: when you encounter this requirement in development, if the objects in an array are in the following format, can you do Cartesian product operation according to the difference of sidePos to get the following strings of different sidePos fields? The test field does not have any meaning, but only indicates the field name

item.value = `{"test": {"sidePos": "", "val": 0}, {"sidePos": "", "val": 1}}`
item.value = `{"test": {"sidePos": "", "val": 0}, {"sidePos": "", "val": 4}}`
item.value = `{"test": {"sidePos": "", "val": 2}, {"sidePos": "", "val": 1}}`
item.value = `{"test": {"sidePos": "", "val": 2}, {"sidePos": "", "val": 4}}`
item.value = `{"test": {"sidePos": "", "val": 3}, {"sidePos": "", "val": 1}}`
item.value = `{"test": {"sidePos": "", "val": 3}, {"sidePos": "", "val": 4}}`

beginners ask for advice ~

Mar.02,2021

  1. put "hundred" in one array and "ten" in another
  2. the result array of two-layer loops combined into nimm elements

pseudo code:

for each i in 
    for each j in 
        result.push(i + j)

another way of implementation:

  1. two-layer loop. Determine whether the two elements are the same in the inner loop. If they are not the same, they are combined. If the same is the same, skip
.

pseudo code:

for each i in arr
    for each j in arr
        if (i.sidePos != j.sidePos)
            result.push(i + j)

I don't understand what you are

Menu