Home > Blockchain >  How to concatenate two arrays using for loops on javascript?
How to concatenate two arrays using for loops on javascript?

Time:12-30

I want to merge two arrays through for loops and assign them into a new array.
I have these two arrays:

const drivers = ['Verstappen', 'Hamilton', 'Raikkonen', 'Bottas', 'Lando Norris', 'Leclerc', 'Ricciardo', 'Vettel', 'Stroll', 'Tsunoda'];
const livery = ['Red Bull', 'Mercedes-Benz', 'McLaren', 'Ferrari', 'Aston Martin', 'Alpha Tauri'];

I want to assign each one of these 'drivers' a livery (not necessarily the real one they represent irl), but every time I try to tweak my code I end up assigning each driver all the liveries when I only want to give them one or I manage to get only the last 'driver' on the array with the last 'livery'.

All the versions of my code usually revolve around this:

const drivers = ['Verstappen', 'Hamilton', 'Raikkonen', 'Bottas', 'Lando Norris', 'Leclerc', 'Ricciardo', 'Vettel', 'Stroll', 'Tsunoda'];
const livery = ['Red Bull', 'Mercedes-Benz', 'McLaren', 'Ferrari', 'Aston Martin', 'Alpha Tauri'];
let resultArray = [];

const loopIndex = (arrays, arrays2) => {
    for (i = 0; i < arrays.length; i  )
        for (j = 0; j < arrays2.length; j  )
            resultArray = arrays[i].concat(arrays2[j]);
}; 

loopIndex(drivers, livery);
console.log(resultArray); //Inputs TsunodaAlpha Tauri

Expected Output:

resultArray = ['VerstappenRedBull','HamiltonMercedes Benz','RaikkonenMcLaren','BottasFerrari','Lando NorrisAston Martin','LeclercAlpha Tauri','RicciardoRed Bull','VettelMercedes-Benz','StrollMcLaren','TsunodaFerrari'];

Or something similar I just wanted to see if i could concatenate one driver with one livery using for loops.

CodePudding user response:

If you want to use a for loop, you could do something like:

const drivers = ['Verstappen', 'Hamilton', 'Raikkonen', 'Bottas', 'Lando Norris', 'Leclerc', 'Ricciardo', 'Vettel', 'Stroll', 'Tsunoda'];

const livery = ['Red Bull', 'Mercedes-Benz', 'McLaren', 'Ferrari', 'Aston Martin', 'Alpha Tauri'];

const resultArray = [];

for (let i = 0; i < drivers.length; i  ) {
  const j = i % livery.length;

  resultArray.push(`${drivers[i]} | ${livery[j]}`);
}

/*
or the slightly more compact version, using forEach:

drivers.forEach((driver, index) => {
  const j = index % livery.length;
  
  resultArray.push(`${driver} | ${livery[j]}`);
})
*/

console.log(resultArray);

Anyway I would suggest using Array.prototype.map():

const drivers = ['Verstappen', 'Hamilton', 'Raikkonen', 'Bottas', 'Lando Norris', 'Leclerc', 'Ricciardo', 'Vettel', 'Stroll', 'Tsunoda'];

const livery = ['Red Bull', 'Mercedes-Benz', 'McLaren', 'Ferrari', 'Aston Martin', 'Alpha Tauri'];

const resultArray = drivers.map((driver, index) => {
  const j = index % livery.length;
  
  return `${driver} | ${livery[j]}`
});

console.log(resultArray)

CodePudding user response:

One way to do this is as follows

const drivers = ['Verstappen', 'Hamilton', 'Raikkonen', 'Bottas', 'Lando 
    Norris', 'Leclerc', 'Ricciardo', 'Vettel', 'Stroll', 'Tsunoda'];
const livery = ['Red Bull', 'Mercedes-Benz', 'McLaren', 'Ferrari', 'Aston 
    Martin', 'Alpha Tauri'];
let resultArray = [];

const loopIndex = (arrays, arrays2) => {
    console.log(arrays,arrays2);
    for (i = 0; i < arrays.length; i  )
        resultArray.push(arrays[i])
    for (j = 0; j < arrays2.length; j  )
        resultArray.push(arrays2[j])
}; 

loopIndex(drivers, livery);
console.log(resultArray); //Inputs TsunodaAlpha Tauri

//result will be as follow (final array containing 16 items, 10 from first array and 6 from second array)

  [1]: https://i.stack.imgur.com/sgrUk.png

CodePudding user response:

As per the Output requirement, you need to do the following

const drivers = ['Verstappen', 'Hamilton', 'Raikkonen', 'Bottas', 
'Lando Norris', 'Leclerc', 'Ricciardo', 'Vettel', 'Stroll', 'Tsunoda'];
    const livery = ['Red Bull', 'Mercedes-Benz', 'McLaren', 'Ferrari', 
'Aston Martin', 'Alpha Tauri'];
     let resultArray = [];

     const loopIndex = (arrays, arrays2) => {
        arrays.forEach((arrayItem, i) =>
        {
            const j = i % arrays2.length;
            resultArray.push(`${arrayItem}${arrays2[j]}`)
        });
    }; 

loopIndex(drivers, livery);
console.log(resultArray); //Inputs TsunodaAlpha Tauri
  • Related