2

With an input of a 2 dimensions array I need to get as output an array with the elements in uppercase.

This is my try, but it doesn't works.

var cityColumn = [['avila'], ['burgos'], ['madrid'], ['sevilla']];
var cityRow = [['avila', 'avila', 'burgos', 'madrid', 'sevilla']];
var cityCell = [['sevilla']];


console.log(cityRow);
function upperCaseArray(myArray) {
  var upperized = myArray.map(function(city){
    console.log(typeof city);
    return city.toUpperCase();
  });
  return upperized;
}

console.log(upperCaseArray(cityColumn));
console.log(upperCaseArray(cityRow));
console.log(upperCaseArray(cityCell));
// output desired:
// [['AVILA], ['BURGOS'], ['MADRID'], ['SEVILLA']]
// [['AVILA, 'AVILA', 'BURGOS', 'MADRID', SEVILLA']]
// [['SEVILLA']]

Note: thesee inputs are that I've get from a Google Sheet range SpreadsheetApp.getActiveSpreadsheet().getSelection().getActiveRange().getValues(). I'm starting coding Google Apps Script.

0

8 Answers 8

4

Because your strings are nested inside arrays which are inside arrays themselves, you need two .maps:

var cityColumn = [['avila'], ['burgos'], ['madrid'], ['sevilla']];
var cityRow = [['avila', 'avila', 'burgos', 'madrid', 'sevilla']];
var cityCell = [['sevilla']];
function upperCaseArray(arr) {
  return arr.map(function(subarr) {
    return subarr.map(function(str) {
      return str.toUpperCase();
    });
  });
}
console.log(upperCaseArray(cityColumn));
console.log(upperCaseArray(cityRow));
console.log(upperCaseArray(cityCell));

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

Comments

1

var cityColumn = [['avila'], ['burgos'], ['madrid'], ['sevilla']];
var cityRow = [['avila, avila, burgos, madrid, sevilla']];
var cityCell = [['sevilla']];


console.log(cityRow);
function upperCaseArray(arr) {
  return arr.map(a => a.map(item => item.toUpperCase()));
  }

console.log(upperCaseArray(cityColumn));
console.log(upperCaseArray(cityRow));
console.log(upperCaseArray(cityCell));

Comments

1

First off, your elements in the arrays need to be enclosed with quotes " or ' to mark them as strings, otherwise the interpretator will see them as undefined variables.

You can use the map function to apply a function to all elements in an array. But since this is a 2 dimensional array you need to apply it in a nested way, like the following:

var cityColumn = [["avila"], ["burgos"], ["madrid"], ["sevilla"]];
var cityRow = [["avila", "avila", "burgos", "madrid", "sevilla"]];
var cityCell = [["sevilla"]];

function arrUpper(arr) {
    // o as in outer, and i as in inner
    return arr.map(o => o.map(i => i.toUpperCase()));
}

console.log(arrUpper(cityColumn));
console.log(arrUpper(cityRow));
console.log(arrUpper(cityCell));

Output

[["AVILA"], ["BURGOS"], ["MADRID"], ["SEVILLA"]]
[["AVILA", "AVILA", "BURGOS", "MADRID", "SEVILLA"]]
[["SEVILLA"]]

1 Comment

IIRC, Rhino (GAS) doesn't support ES6 syntax, unfortunately
1

I had to add single quotes to strings.

var cityColumn = [['avila'], ['burgos'], ['madrid'], ['sevilla']];
var cityRow = [['avila, avila, burgos, madrid, sevilla']];
var cityCell = [['sevilla']];


console.log(cityRow);
function upperCaseArray(arr) {
  return arr.map(a => a.map(item => item.toUpperCase()));
  }

console.log(upperCaseArray(cityColumn));
console.log(upperCaseArray(cityRow));
console.log(upperCaseArray(cityCell));

3 Comments

IIRC, Rhino (GAS) doesn't support ES6 syntax, unfortunately
I'm not following. Why should it support Rhino (GAS)?
Because OP is using GAS (which runs on Rhino)
1

You can join() the array to make the array as a string. Then upper case the string. Finally split() them to from the array again.

Change

return city.toUpperCase();

To

return city.join(',').toUpperCase().split(',');

var cityColumn = [['avila'], ['burgos'], ['madrid'], ['sevilla']];
var cityRow = [['avila', 'avila', 'burgos', 'madrid', 'sevilla']];
var cityCell = [['sevilla']];

function upperCaseArray(myArray) {
  var upperized = myArray.map(function(city){
    return city.join(',').toUpperCase().split(',');
  });
  return upperized;
}

console.log(upperCaseArray(cityColumn));
console.log(upperCaseArray(cityRow));
console.log(upperCaseArray(cityCell));

Comments

1

You can use map recursively.

function toUpper(arr){
  if(arr.map){
   return arr.map(toUpper);
  } else {
   return arr.toUpperCase();
  }
}

Recursion depth for a two dimensional array is 2. GAS supports upto 1000.

Comments

0
var cityColumn = [['avila'], ['burgos'], ['madrid'], ['sevilla']];
var cityRow = [['avila', 'avila', 'burgos', 'madrid', 'sevilla']];
var cityCell = [['sevilla']];

function parseData(input){
        return input.reduce(function(o,i){
            return i.reduce(function(oo,ii){
                oo.push(ii.toUpperCase());
                return oo;
            },[]);
        },[]);
    }

console.log(parseData(cityCell));
console.log(parseData(cityRow));
console.log(parseData(cityColumn));

5 Comments

.map is probably more appropriate than reduce when you're transforming every element in one array into an element in another array
.map will change the original object itself that not what you want.
No, .map does not mutate the original array. (none of the array methods mutate except for splice, push, pop, shift, unshift, sort, reverse IIRC)
if it's not mutate then it will also create the new object
@CertainPerformance In terms of performance both are more or less same. Better to go with for loop. Found same good performance metrics on github github.com/dg92/Performance-Analysis-JS . Have a look into it.
0

You can use .map() to create arrays with uppercase strings:

let cityColumn = [['avila'], ['burgos'], ['madrid'], ['sevilla']];
let cityRow = [['avila', 'avila', 'burgos', 'madrid', 'sevilla']];
let cityCell = [['sevilla']];

function upperCase(arr) {
   return arr.map(function(a) {
      return a.map(function(s) { return s.toUpperCase(); });
   });
};

console.log(upperCase(cityColumn));
console.log(upperCase(cityRow));
console.log(upperCase(cityCell));
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

@CertainPerformance Thanks for the input. I've updated my answer accordingly.

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.