0

I have a question that follows this thread. Its a follow up to this answer.

Google Apps Script Spreadsheets - Write Array to cells

how do i get

var employees=["Adam","Barb","Chris"];

to look like this?

var employees=[["Adam"],["Barb"],["Chris"]];
Community
  • 1
  • 1
jason
  • 3,811
  • 18
  • 92
  • 147

2 Answers2

4

You could use map():

var employees=["Adam","Barb","Chris"];

var newEmployees = employees.map( function( item ){ return [ item ]; } );
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • @HMR But a suitable shim is given on MDN as well. So just add the shim and you should be alright. – Sirko Feb 03 '13 at 13:36
  • Yes, here is the map for browsers that don't implement it: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map#Compatibility Just needed mentioning if you need support for IE<9 – HMR Feb 03 '13 at 13:41
  • @HMR Did you notice, that this link is already part of my answer? – Sirko Feb 03 '13 at 13:42
2
var employees = ["Adam", "Barb", "Chris"];

for (var i = employees.length; i--;) {
    employees[i] = [employees[i]];
}
David G
  • 94,763
  • 41
  • 167
  • 253