-1

For example I have an array like this

var arr = [
    ['Red', 'Black'],
    ['S', 'M'],
    ['Cotton', 'Linen']
]

How to have the result with an array likes:

result: [
   'Red - S - Cotton',
   'Red - M - Cotton',
   'Red - S - Linen',
   'Red - M - Linen',
   'Black - S - Cotton',
   ...
]

This is the code I coded before, but it not work as I want :( the result is not good as I want https://jsfiddle.net/minhthe208/shkz7av3/47/ (please see the console, it's duplicated name) and miss some names, for example: Black - M - Cotton is missed

    var arr = [
          ['Red', 'Black'],
          ['S', 'M'],
          ['Cotton', 'Linen']
        ]
        
        var sum = 1;
        for (var i = 0; i < arr.length; i++) {
          sum *= arr[i].length;
        }
        
        function FullRow(arr, len) {
          var index = 0
          var result = []
          var current = 0
          while (index < len) {
            if (current === arr.length)
              current = 0
            result[index] = arr[current]
            index++
            current++
          }
          return result
        }
        
        function Combine(arr, len) {
          var result = arr[0];
          for (var index = 1; index < arr.length; index++) {
            for (var current = 0; current < len; current++) {
              result[current] += ' - ' + arr[index][current];
            }
          }
          return result;
        }
        
        var combineArr = [];
        for (var i = 0; i < arr.length; i++) {
          combineArr.push(FullRow(arr[i], sum));
        }
        
        console.log('----COMBINE----')
        var finalArr = Combine(combineArr, sum)
        
        console.log(finalArr)

What should I do to have the result as above?

Ryan can fly
  • 123
  • 1
  • 11
  • Post your code here. – possum Oct 29 '22 at 16:34
  • @possum the forum didn't allow me do this, please go to the jsfiddle link https://jsfiddle.net/minhthe208/shkz7av3/49/ – Ryan can fly Oct 29 '22 at 16:35
  • 1
    No. You shouldn't assume people are willing to click through to a different site to help you. If you are not able to post the code here for some reason, then you need to address that problem. – possum Oct 29 '22 at 16:36
  • @possum thanks, finally I can edit the post. please see my code above – Ryan can fly Oct 29 '22 at 16:38
  • Will you always have 3 sub-arrays? – Cid Oct 29 '22 at 16:44
  • @Cid Hi Cid, No, it's just an example. It may changed depend on the user input – Ryan can fly Oct 29 '22 at 16:45
  • See [How can I create every combination possible for the contents of two arrays?](/q/8936610/4642212). – Sebastian Simon Oct 29 '22 at 17:54
  • @SebastianSimon thanks for your reply, but my problem is really difference situation. 1) I'm not sure how much sub array in the parent array, and 2) every arrays not the same length – Ryan can fly Oct 29 '22 at 18:05
  • @ThiếtKếWebsiteMinhThế The first point is not an issue: at least one answer there covers the case of an “arbitrary number of arrays”. The second point shouldn’t be an issue either, but it depends on what you plan to do with mismatching array lengths. – Sebastian Simon Oct 29 '22 at 19:15

1 Answers1

1

const array = [
  ["Red", "Black"],
  ["S", "M"],
  ["Cotton", "Linen"],
];

const result = (...a) =>
  a.reduce((a, b) => a.flatMap((d) => b.map((e) => `${d},${e}`)));

let output = result(...array);
console.log(output);
Bimal Pariyar
  • 257
  • 2
  • 9
  • You're really at a difference level with mine, wish that one day I could do the same and could be the logic as you. Thanks you @Bimal Pariyar – Ryan can fly Oct 29 '22 at 17:12