1

I have the following array of arrays:

    [
      [ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1 & 2' ],
      [ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3 & 4' ],
      [ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5 & 6' ]
    ]

I have to split the second item of every nested array and create two new arrays with them and the first item of a nested array like this:

    [
       [
          [ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1' ],
          [ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '2' ]
       ],
       [
          [ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3' ],
          [ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '4' ]
       ],
       [
          [ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5' ],
          [ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '6' ]
       ]
    ]

I need this kind of nested array because I use it for a bulk insert in nodeJS.

My current method that I am using to create this array looks like this:

    console.log(teams.map(team => {
            let initialArray = []
            let firstPlayer = [uuidv4(), team[0], team[1].split(' & ')[0]]
            let secondPlayer = [uuidv4(), team[0], team[1].split(' & ')[1]] 
    
            data = (initialArray.concat(firstPlayer)).concat(secondPlayer)
    
            return data
        }))

And the response is like that:

    [
      [
        'da4d27fb-06e3-4075-aaba-be799d793bbd',
        '3b5ec76c-a501-4edf-b97c-2526d6d3e147',
        '1',
        'df869f69-76fb-41bc-af59-f86c1c623cfb',
        '3b5ec76c-a501-4edf-b97c-2526d6d3e147',
        '2'
      ],
      [
        'fb7c5620-6f20-4586-a82a-30c0ea39022e',
        '1f7a07f4-45aa-4a35-ad42-d3f187ee804a',
        '3',
        '1d30d9d7-3d63-4309-a7d3-5bc6abe17500',
        '1f7a07f4-45aa-4a35-ad42-d3f187ee804a',
        '4'
      ],
      [
        '433159a1-ee21-4f91-ae6e-69fb359b747b',
        'c8a4eead-79ee-4d4b-a16d-68723798d338',
        '5',
        '8321a976-9bae-408d-810b-f5508012d58e',
        'c8a4eead-79ee-4d4b-a16d-68723798d338',
        '6'
      ]
    ]

I have also tried a method using reduce found in this stackoverflow question: Return multiple arrays using "map" function

If somebody knows a method for this, or you need further explanations, let me know.

Thank you!

3 Answers 3

1

Try this approach

const data = [[ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1 & 2' ],[ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3 & 4' ],[ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5 & 6' ]];

const result = data.map(([guid, teams]) => {
    const [team1, team2] = teams.split(' & ');
    return [[guid, team1], [guid, team2]];
});

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

Same result but with oneliner

const data = [[ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1 & 2' ],[ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3 & 4' ],[ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5 & 6' ]];

const result = data.map(([guid, teams]) => teams.split(' & ').map((team) => [guid, team]));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

Sign up to request clarification or add additional context in comments.

Comments

1

This would also work.

Using Array.prototype.reduce()

const input = [
  [ '3b5ec76c-a501-4edf-b97c-2526d6d3e147', '1 & 2' ],
  [ '1f7a07f4-45aa-4a35-ad42-d3f187ee804a', '3 & 4' ],
  [ 'c8a4eead-79ee-4d4b-a16d-68723798d338', '5 & 6' ]
]

const output = input.reduce((prev, [uuid, combinedValue]) => {
  const [first,second] = combinedValue.split(" & ");
  prev.push([[uuid, first], [uuid, second]]);
  return prev;
},[])

console.log(output);

Comments

1

You Just need 3 lines of Code

const source = [
  ["3b5ec76c-a501-4edf-b97c-2526d6d3e147", "1 & 2"],
  ["1f7a07f4-45aa-4a35-ad42-d3f187ee804a", "3 & 4"],
  ["c8a4eead-79ee-4d4b-a16d-68723798d338", "5 & 6"],
];

const destination = source.map(team => {
    const options = team[1].split('&'); 
    newSubDestinationArray = options.map(item => ["your uuid id", team[0], item]); 
    return newSubDestinationArray;
})


console.log(destination)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.