1

I have an array of URLs pulled from the files in a folder, which I need to add to another array.

Array1 = [url1 , url2 , url3]

Array2 = [ [1 , 2] , [3 , 4] , [5 , 6] ]

I need the URL's to be distributed into each index, like:

Array2 = [ [url1 , 1 , 2] , [url2 , 3 , 4] , [url3 , 5 , 6] ]

Do I need to use a for loop, or concat, I'm unsure.

I've tried Array2.push([Array1]), but end up with all of Array1 in the first index position, rather than distributed through the array.

1 Answer 1

1

Use map

Array2 = Array2.map( ( s, i ) => ( s.unshift( Array1[ i ] ), s ) );

Demo

var Array1 = ["url1" , "url2" , "url3"]

var Array2 = [ [1 , 2] , [3 , 4] , [5 , 6] ];

Array2 = Array2.map( ( s, i ) => ( s.unshift( Array1[ i ] ), s ) );

console.log( Array2 );

Edit

Equivalent function without arrows

Array2 = Array2.map( function( s, i ){
  s.unshift( Array1[ i ] );
  return s;
});
Sign up to request clarification or add additional context in comments.

3 Comments

For some reason the google script editor gives me a syntax error if I try to use Array2 = Array2.map( ( s, i ) => ( s.unshift( Array1[ i ] ), s ) );
Is the arrow function not supported in your browser? If not, I can give a normal function.
I think its the arrow function, I'm using chrome, maybe google doesn't support it?

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.