0

I feel like this should be easier to find online, but I haven't been able to find anything about it.

So my question is, can I use the total amount from an existing array to build that many separate arrays and auto populate them?

Can you Build Multiple Arrays using a For Loop based on the value amount of an existing array?

var arrA = ["box 1", "box 2", "box 3"]; 

for (i = 0; i < arrA.length; i++) {
  var arr[i] =[
    "Style[i]",
    "ListA[i]",
    "ListB[i]"
  ];
}

This doesn't work, but the result I am looking for is an output like this

var arr1 = ["Style1","ListA1","ListB1"];
var arr2 = ["Style2","ListA2","ListB2"];
. . . .

and continue repeating for the amount of times based on the Array length?

13
  • 1
    What's the expected output or result ? Commented May 11, 2020 at 0:25
  • So when I add "box 4" to the first array, it will automatically generate the next array, rather than having to write the entire thing out each time. Commented May 11, 2020 at 0:30
  • 2
    What is the next array ? What is the expected result ? Commented May 11, 2020 at 0:31
  • 1
    You question is unclear, if you want a response please clarify your question. See How to Ask and provide additional information like how many other arrays you want, what should theses array contain and how they are related with the first one. Commented May 11, 2020 at 0:36
  • 4
    var arrA = ["box 1", "box 2", "box 3", "box 4"]; done. Commented May 11, 2020 at 0:40

4 Answers 4

1

You are looking for this ?

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 )

Sign up to request clarification or add additional context in comments.

2 Comments

This sort of works, but I'd need them to come out as arrays, Arr1 = ["style1","ListA1","ListB1"]; Arr2 = ["style2","ListA2","ListB2"]; etc..
@Guardian_808 bad idea, in this case prefer to use a meta object tou build your array list
0

What about a function that takes the original, and the new array in, and returns the result.

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.

Comments

0

If I understood you right, you want to dynamically create some arrays from a given array. I think you can do it this way

let arrA = ["box 1", "box 2", "box 3"]; 
let generatedArrays = {};
for (let i = 0 ; i< arrA.length ; i++){
 generatedArrays[arrA[i]] = []
 }
 console.log(generatedArrays)

1 Comment

Not quite what I'm aiming for, sorry.
0

You can implement this by creating two functions, one to initialize the first list of arrays (called once, at the beginning) and the one one is called every time you add a new item to the first array as so:

//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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.