简体   繁体   English

如何使用 javascript 上的循环连接两个 arrays?

[英]How to concatenate two arrays using for loops on javascript?

I want to merge two arrays through for loops and assign them into a new array.我想通过 for 循环合并两个 arrays 并将它们分配到一个新数组中。
I have these two arrays:我有这两个 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:预期 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.或者类似的东西,我只是想看看我是否可以使用 for 循环将一个驱动程序与一种涂装连接起来。

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) //结果如下(最终数组包含 16 个项目,第一个数组中的 10 个,第二个数组中的 6 个)

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

If you want to use a for loop, you could do something like:如果要使用 for 循环,可以执行以下操作:

 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() :无论如何,我建议使用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)

As per the Output requirement, you need to do the following根据 Output 要求,您需要执行以下操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM