用JavaScript映射二维数组

时间:2018-11-07 09:20:50

标签: javascript arrays string google-apps-script map-function

使用2维数组的输入时,我需要将元素大写的数组作为输出。

这是我的尝试,但不起作用。

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']]

注意:这些信息是我从Google表格范围SpreadsheetApp.getActiveSpreadsheet().getSelection().getActiveRange().getValues()获得的。我正在开始编写Google Apps脚本的代码。

8 个答案:

答案 0 :(得分:4)

由于您的字符串嵌套在数组本身内部的数组中,因此需要两个 .map

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

答案 1 :(得分:1)

首先,数组中的元素需要用引号"'括起来以将它们标记为字符串,否则解释器会将它们视为未定义的变量。

您可以使用map函数将函数应用于数组中的所有元素。但是,由于这是二维数组,因此您需要以嵌套方式应用它,如下所示:

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

输出

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

答案 2 :(得分: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));

答案 3 :(得分: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));

答案 4 :(得分:1)

您可以Please go through this link it may help: https://mapr.com/support/s/article/Optimizing-Hive-cross-joins?language=en_US Hive supports map-joins only for inner, left and right outer joins. To make sure a product join happens with a map joins, we may have to fake an inner join to do product join. We have to change the split size to say 5 MB and write the query like below. The cross join of the 1,2 in sub queries will now be distributed across all 60 mappers. set mapreduce.input.fileinputformat.split.maxsize=5000000 with Table1 AS (Select value, 1 as key from A), Table2 AS (Select value,1 as key from B) Select Table1.A1, min(Table1.value * Table2.value) from Table1 inner join Table2 on (Table1.key=Table2.key) group by Table1.A1 Kindly refer to the link for reference 将该数组制成字符串。然后大写字符串。最后,再次join()将它们从阵列中移出。

更改

split()

收件人

return city.toUpperCase();

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

答案 5 :(得分:1)

您可以递归使用map

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

二维数组的递归深度为2。GAS最多支持1000。

答案 6 :(得分:0)

您可以使用.map()创建带有大写字符串的数组:

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; }

答案 7 :(得分: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));