-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?

5
  • Really depends on what your use case is when you say "combine them" and what you are trying to accomplish at a higher level Commented Sep 13, 2018 at 21:27
  • 2
    You can just spread them: let main = [...array1, ...array2, ...array3] Commented Sep 13, 2018 at 21:28
  • @charlietfl the duplicate is a duplicate too, why don't you point to the original? stackoverflow.com/questions/1584370/… Commented Sep 13, 2018 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 Commented Sep 13, 2018 at 21:43
  • @MarkMeyer that helped me Commented Sep 14, 2018 at 15:49

1 Answer 1

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 ?`

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.