我觉得应该可以更容易地在网上找到它,但是我一直找不到任何东西。
所以我的问题是,我可以使用现有阵列中的总数来构建那么多单独的阵列并自动填充它们吗?
var arrA = ["box 1", "box 2", "box 3"];
for (i = 0; i < arrA.length; i++) {
var arr[i] =[
"Style[i]",
"ListA[i]",
"ListB[i]"
];
}
这不起作用,但是我正在寻找的结果是这样的输出
var arr1 = ["Style1","ListA1","ListB1"];
var arr2 = ["Style2","ListA2","ListB2"];
. . . .
根据数组长度继续重复多少时间?
答案 0 :(得分:1)
您在寻找这个吗?
const arrA = ["box 1", "box 2", "box 3"];
const arrB = arrA.map((c,i)=>[`Style${i+1}`,`ListA${i+1}`,`ListB${i+1}`])
console.log( arrB )
答案 1 :(得分:0)
将原始数组和新数组放入并返回结果的函数怎么样?
let addArray = (original, newArray) => {
let result = original.push(newArray)
return result
}
//Not sure what's meant by auto-populating them...But this
//Will take an array of arrays, and simply push "newArray" into
// "original" array of arrays.
答案 2 :(得分:0)
如果我理解正确,那么您想从给定数组动态创建一些数组。 我想你可以这样做
let arrA = ["box 1", "box 2", "box 3"];
let generatedArrays = {};
for (let i = 0 ; i< arrA.length ; i++){
generatedArrays[arrA[i]] = []
}
console.log(generatedArrays)
答案 3 :(得分:0)
您可以通过创建两个函数来实现此目的,一个函数初始化数组的第一个列表(在开始时调用一次),每次在第一个数组中添加新项时都调用一个: >
//first (master) array
var arrA = ["box 1", "box 2", "box 3"];
//array to hold individual box's array
var arrBoxes = []
function createInitArrays() {
for (i = 0; i < arrA.length; i++) {
arrBoxes[i] = [
"Style" + (i + 1),
"ListA" + (i + 1),
"ListB" + (i + 1)
];
}
}
function createNewArray() {
var index = arrA.length
arrBoxes[index] = [
"Style" + (index+1),
"ListA" + (index+1),
"ListB" + (index+1)
];
}
//call to create initial arrays
createInitArrays()
// call to create new array every time you update 'arrA'
// createNewArray()
// display list of arrays based on 'arrA'
console.log(arrBoxes)