0

I have two methods of writing the same function using Map(), from my reading I can not tell if there is a best practices method

Related to this are nested ternary operations considered bad practice?

Method2

function Method2() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();  
  var sht = ss.getSheetByName('Elements');  
  var rng = sht.getDataRange();
  var values = rng.getValues();
  
  values = values.map(x => x.map(y => giveNumber(y)));
  
  sht.getRange(1,1, values.length, values[0].length).setValues(values);
}


function giveNumber(str) {  
  if(str=="Networking - 1") {str=1}
  else if(str=="Cooperating - 2") {str=2}
  else if (str=="Collaborating - 3"){str=3}
  else if(str=="Partnering - 4") {str=4}
  else {str=str}
  return str
}

Method3

function Method3() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();  
  var sht = ss.getSheetByName('Elements');  
  var rng = sht.getDataRange();
  var values = rng.getValues();
  
  values = values.map(x => x.map(y => y==("Networking - 1") ? 1: 
                                      y==("Cooperating - 2") ? 2: 
                                      y==("Collaborating - 3") ? 3: 
                                      y==("Partnering - 4") ? 4: 
                                      y));
  
  
  sht.getRange(1,1, values.length, values[0].length).setValues(values);
}

Marios
  • 26,333
  • 8
  • 32
  • 52
xyz333
  • 679
  • 6
  • 14

2 Answers2

2

I disagree with the accepted answer. Ternary operators can be used effectively for multiple if/else statements. If used with proper code intents/style, it doesn't harm readability and is shorter.

const mapFunc = y =>
  y == 'Networking - 1'
    ? 1
    : y == 'Cooperating - 2'
    ? 2
    : y == 'Collaborating - 3'
    ? 3
    : y == 'Partnering - 4'
    ? 4
    : y;

Variability does exist in performance as well. Although this is not tested in newer engine, In older engine, ternary operators were the fastest.

Given multiple conditions on the same variable, it is also apt to use switch statements instead.


In your specific case however, If you're just trying to get the last number in the string, You could just get the last character or use regex:

y => Number(y[y.length-1]) || y

Another alternative is to use Arrays with indexOf :

const strs = [
  'Networking - 1',
  'Cooperating - 2',
  'Collaborating - 3',
  'Partnering - 4',
];
const mapFunc = y => strs.indexOf(y) + 1 || y;

Also see:

Ternary operators in JavaScript without an "else"

TheMaster
  • 45,448
  • 6
  • 62
  • 85
1

When you have multiple conditions to check, ternary operators can not be used efficiently.

It is recommended to use an if/else if statement:

values = values.map(x => x.map(y =>
                    {
                    if (y== "Networking - 1"){
                    return 1;
                    }
                    else if(y=="Cooperating - 2"){
                    return 2;
                    }
                    else if(y=="Collaborating - 3"){
                    return 3;
                    }
                    else {
                    return 4;                     
                    }                
                    }
 ));
Marios
  • 26,333
  • 8
  • 32
  • 52