0

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 ?

EDIT:

i decided to use this

var output = [];

for(var a = 0; a <= array1.length; a++)
output.push([array1[a],array2[a]]);

how would this compare to map function, performancewise ?

  • Related: https://stackoverflow.com/questions/63720612/what-does-the-range-method-getvalues-return-and-setvalues-accept – TheMaster Nov 15 '21 at 22:45

2 Answers2

1

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

Cooper
  • 59,616
  • 6
  • 23
  • 54
0
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}]

  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Jean-Francois Gagnon Nov 15 '21 at 22:29
  • This is not a [tag:python] question. – TheMaster Nov 15 '21 at 22:41
  • @Jean-FrancoisGagnon There's really not much to say about it. The op showed us what we were starting with and what was wanted. I struggled to find anything to say.. – Cooper Nov 15 '21 at 23:36