如何将一个数组推到另一个数组中

How can i push a array in another array?

本文关键字:数组 另一个 一个      更新时间:2024-01-09

我有一个64个位置的主数组,每个位置我都需要另一个数组。如:CCD_ 1。

如何创建这些数组?以及如何获取someObject.name , someObject.lastName

这是代码:

$scope.filaCores = []; 
$scope.object = {} 
$scope.object.name = "name"; 
$scope.object.secondname= "secondname"; 
for(var i = 0; i <10; i++) { 
   $scope.filaCores[i] = [$scope.object.name, $scope.object.secondname]; 
} 

开始:https://jsfiddle.net/jsg81gg8/1/

根据您的描述,我认为不需要数组。但既然这是你的要求,你就走吧。您没有指定子数组的数组元素数量,所以我将展示一个包含三个的示例。如果总共只需要64个结构,则不需要内部数组。

window.getRandomInt = function() {
    return Math.floor(Math.random() * (10000 - 1 + 1)) + 1;
}
mainArray = [];  /* you said you wanted 64 of these */
subArray = [];  /* you didn't specify how many of these, the example below will assume 3 per each mainarray */
for (i = 0; i < 64; i++) {
    for (j = 0; j < 3; j++) {
        /* I'm using getRandomInt() just so you can see all the names are different */
        subArray[j] = {name:"John"+getRandomInt(), lastname:"Doe"+getRandomInt()};
        }
    mainArray[i] = subArray;
}
/* Press F12 and go to the console tab, run this script, and you will see the output of the entire array */
console.log(mainArray);
/* You can access a specific element like this... */
alert('Alerting mainArray[23][2].lastname: '+mainArray[23][2].lastname);

如果你真的不需要子数组,只需要64个结构,那么它可以简化为这样:https://jsfiddle.net/Ldafbwbk/1/

更新:这里有第三个更像您更新的问题的例子:https://jsfiddle.net/7st8fnw5/3/