如何将数组元素组合成一个新数组?

Posted

技术标签:

【中文标题】如何将数组元素组合成一个新数组?【英文标题】:How to combine array elements into a new array? 【发布时间】:2019-12-05 08:40:05 【问题描述】:

javascript 新手(以及一般的编码,Excel 函数之外),希望有人能帮助我指出正确的方向。我在网上搜索过,但没有找到明确的答案。

我有两个数组 - 一个包含星期几,一个包含人们的名字和姓氏。我从带有人名的数组中选择一个随机索引号,并尝试将名字和姓氏分配给一天,然后将所有三个值放入一个新数组中。然后我将这个最终数组打印到 html 表中。

包含天数的数组是一维的,只是从星期日到星期六。我理解的包含人名的数组实际上是二维的,即他们的名字和姓氏。

我通过硬编码第三个数组来完成这项工作,但我想改为动态创建数组。我似乎无法让 array.push() 方法按我的意愿工作,我希望有人愿意提供帮助。

注意 - 我通过创建另一个数组来选择随机索引号,该数组在 0 和 people 数组的长度之间选择七个随机数。 push 方法在这种情况下可以正常工作。

我尝试过使用 array.push() 方法,但似乎无法按照我的预期工作。它要么返回一个空白值,要么返回一个看起来很乱的东西,尽管我认为它是来自 people 数组的随机字母。

//This is my working code.  The script tag is after the body elements because I need to create some of the body elements before I run the script.

 <body>        
    <table id="Week1Table" class="center">
        <tr>
            <th class="Week1">Week 1</th>
            <th class="Week1">First Name</th>
            <th class="Week1">Last Name</th>
        </tr>
    </table>

    <table id="Week2Table" class="center">
        <tr>
            <th class="Week2">Week 2</th>
            <th class="Week2">First Name</th>
            <th class="Week2">Last Name</th>
        </tr>
    </table>

    <script>            
        var people = [
            ["Andre", "the Giant"],
            ["Randy", "Savage"],
            ["Steve", "Austin"],
            ["The", "Rock"],
            ["Man", "Kind"],
            ["Steel", "Cage"],
            ["I", "Quit"]
        ];
        var week1schedule = [
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday"
        ];

        var week2schedule = week1schedule;

        var peoplePicker = []
        while(peoplePicker.length < week1schedule.length*2)
            var r = Math.floor(Math.random()*people.length);
            if(peoplePicker.indexOf(r) === -1) peoplePicker.push(r);
        ;


//This is the final array, and the one that I would like to create dynamically rather than statically.

        var peopleRandomizerTable = [];
        peopleRandomizerTable[0] = week1schedule[0];
        peopleRandomizerTable[1] = people[peoplePicker[0]][0];
        peopleRandomizerTable[2] = people[peoplePicker[0]][1];
        peopleRandomizerTable[3] = week1schedule[1];
        peopleRandomizerTable[4] = people[peoplePicker[1]][0];
        peopleRandomizerTable[5] = people[peoplePicker[1]][1];
        peopleRandomizerTable[6] = week1schedule[2];
        peopleRandomizerTable[7] = people[peoplePicker[2]][0];
        peopleRandomizerTable[8] = people[peoplePicker[2]][1];
        peopleRandomizerTable[9] = week1schedule[3];
        peopleRandomizerTable[10] = people[peoplePicker[3]][0];
        peopleRandomizerTable[11] = people[peoplePicker[3]][1];
        peopleRandomizerTable[12] = week1schedule[4];
        peopleRandomizerTable[13] = people[peoplePicker[4]][0];
        peopleRandomizerTable[14] = people[peoplePicker[4]][1];
        peopleRandomizerTable[15] = week1schedule[5];
        peopleRandomizerTable[16] = people[peoplePicker[5]][0];
        peopleRandomizerTable[17] = people[peoplePicker[5]][1];
        peopleRandomizerTable[18] = week1schedule[6];
        peopleRandomizerTable[19] = people[peoplePicker[6]][0];
        peopleRandomizerTable[20] = people[peoplePicker[6]][1];
        peopleRandomizerTable[21] = week2schedule[0];
        peopleRandomizerTable[22] = people[peoplePicker[7]][0];
        peopleRandomizerTable[23] = people[peoplePicker[7]][1];
        peopleRandomizerTable[24] = week2schedule[1];
        peopleRandomizerTable[25] = people[peoplePicker[8]][0];
        peopleRandomizerTable[26] = people[peoplePicker[8]][1];
        peopleRandomizerTable[27] = week2schedule[2];
        peopleRandomizerTable[28] = people[peoplePicker[9]][0];
        peopleRandomizerTable[29] = people[peoplePicker[9]][1];
        peopleRandomizerTable[30] = week2schedule[3];
        peopleRandomizerTable[31] = people[peoplePicker[10]][0];
        peopleRandomizerTable[32] = people[peoplePicker[10]][1];
        peopleRandomizerTable[33] = week2schedule[4];
        peopleRandomizerTable[34] = people[peoplePicker[11]][0];
        peopleRandomizerTable[35] = people[peoplePicker[11]][1];
        peopleRandomizerTable[36] = week2schedule[5];
        peopleRandomizerTable[37] = people[peoplePicker[12]][0];
        peopleRandomizerTable[38] = people[peoplePicker[12]][1];
        peopleRandomizerTable[39] = week2schedule[6];
        peopleRandomizerTable[40] = people[peoplePicker[13]][0];
        peopleRandomizerTable[41] = people[peoplePicker[13]][1];  

        var table1 = document.getElementById("Week1Table");
        var table2 = document.getElementById("Week2Table");
        var peopleRandomizerCount = 0;


//This populates the two tables.  Two weeks; one table for each week.

        for(var rowCount = 0; rowCount < 7; rowCount++)
        
        // create a new row
        var newRow = table1.insertRow(table1.length);
        for(var columnCount = 0; columnCount < 3; columnCount++)
            
                // create a new cell
                var cell = newRow.insertCell(columnCount);

                // add value to the cell
                cell.innerHTML = peopleRandomizerTable[peopleRandomizerCount];
                cell.className = "people";
                peopleRandomizerCount++;
            
        

        for(var rowCount = 0; rowCount < 7; rowCount++)
        
        // create a new row
        var newRow = table2.insertRow(table1.length);
        for(var columnCount = 0; columnCount < 3; columnCount++)
            
                // create a new cell
                var cell = newRow.insertCell(columnCount);

                // add value to the cell
                cell.innerHTML = peopleRandomizerTable[peopleRandomizerCount];
                cell.className = "people";
                peopleRandomizerCount++;
            
        
    </script>
</body>

【问题讨论】:

您可以使用 2 个 for 循环在代码中而不是手动创建该表,但我强烈建议您使用对象重写您的二维数组,而不是像 bhuvnesh 回答的那样。 另外,您不必创建另一个数组来存储随机不定值。在分配给表时,您可以通过创建一个函数并调用它(而不是一个额外的数组浮动)从随机索引中选择数据。 【参考方案1】:

名字和姓氏不需要使用二维数组,

您可以简单地使用包含名字和姓氏的一维对象数组,如下所示:

let people = [
             
              "firstName": "Andre", 
              "lastName": "the Giant"
            ,
             
              "firstName": "Randy", 
              "lastName": "Savage"
             
            ]

现在我猜你可能知道如何组合 2 个一维数组。如果没有请告诉我

【讨论】:

以上是关于如何将数组元素组合成一个新数组?的主要内容,如果未能解决你的问题,请参考以下文章

如何将哈希数组组合成一个数组?

如何把2个数组合并为一个数组

如何将两个firebase集合组合成一个新的对象数组

将两个数组 排列组合到一个数组集合 求java 代码

如何将数组内的数组组合成一个数组? [复制]

php 统计二维数组中某个相等值的总个数,并且组合成一个新的数组