Home > OS >  merging 2 arrays into an array with 2 columns
merging 2 arrays into an array with 2 columns

Time:11-16

I want to merge 2 arrays in the following format.

array1 = [ "a" , "b" , "c"]
array2 = [ 1 , 2 , 3]
merged_array = [ {"a",1} , {"b",2} , {"c",3}]

The goal is to use this as values of 2 columns and rewrite this back to google sheet.

is my format correct and if yes how should i merge the arrays as said above ?

CodePudding user response:

array1 = [ "a" , "b" , "c"]
array2 = [ 1 , 2 , 3]
merged_array = []
for index, value in enumerate(array1): merged_array.append({value,array2[index]})
print (merged_array)

-> [{'a', 1}, {'b', 2}, {'c', 3}]

CodePudding user response:

Merging two arrays into and array of arrays

function myFunk() {
  let array1 = ["a", "b", "c"];
  let array2 = [1, 2, 3];
  let a = array1.map((e,i) => {return [e,array2[i]];})
  Logger.log(JSON.stringify(a));
}

Execution log
4:17:09 PM  Notice  Execution started
4:17:08 PM  Info    [["a",1],["b",2],["c",3]]

Array.map()

  • Related