-3

I have 3 Javascript arrays:

array1 = ["data", "data1", "data2"]
array2 = ["data", "data1", "data2"]
array3 = ["data", "data1", "data2"]

How can I combine them all, so that I can simply run a single loop and retrieve values from them.

for (let index = 0; index < mainArray.length; index++) { 

    value1 = mainArray.array1[index];
    value2 = mainArray.array2[index];
    value3 = mainArray.array3[index];   
}

How can I create a the mainArray to accommodate all three javascript arrays, can a complex object or json object be created?

good.learner
  • 121
  • 1
  • 2
  • 13
  • Really depends on what your use case is when you say *"combine them"* and what you are trying to accomplish at a higher level – charlietfl Sep 13 '18 at 21:27
  • 2
    You can just spread them: `let main = [...array1, ...array2, ...array3]` – Mark Sep 13 '18 at 21:28
  • @charlietfl the duplicate is a duplicate too, why don't you point to the original? https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items – Emeeus Sep 13 '18 at 21:36
  • @Emeeus I only joined in on several other people closing with that duplicate. Also, is not always bad pointing at another duplicate. A user can see it also and might get usefull feedback from both – charlietfl Sep 13 '18 at 21:43
  • @MarkMeyer that helped me – good.learner Sep 14 '18 at 15:49

1 Answers1

0

You can do the following.

var array1 = ["data", "data1", "data2"];
var array2 = ["data", "data1", "data2"];
var array3 = ["data", "data1", "data2"];

var mainArray = array1.concat(array2, array3);

Then mainArray will be `["data", "data1", "data2", "data", "data1", "data2", "data", "data1", "data2"]

Is that what you're after ?`

Jason Jong
  • 4,310
  • 2
  • 25
  • 33