Reputation: 2473
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.
Upvotes: 2
Views: 1384
Reputation: 50445
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.
Upvotes: 1
Reputation: 68933
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));
Upvotes: 1
Reputation: 39322
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; }
Upvotes: 0
Reputation: 21
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));
Upvotes: 1
Reputation: 3611
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"]]
Upvotes: 1
Reputation: 21
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));
Upvotes: 1
Reputation: 2056
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));
Upvotes: 0
Reputation: 370689
Because your strings are nested inside arrays which are inside arrays themselves, you need two .map
s:
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));
Upvotes: 4