Apps Script公式リファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2019年12月10日火曜日

JSで配列から空の要素を除きたい


このような配列を

[1, 2, , 3, 4, '', , , , 5]


こうしたい

[1, 2, 3, 4, 5]





コード.gs
function myFunction() {
  var array = [1, 2, , 3, 4, '', , , , 5];
  var resultArray = array.filter(function(value) { return value !== ''; });
  Logger.log(resultArray)
}



別の書き方

コード.gs
function myFunction() {
  var array = [1, 2, , 3, 4, '', , , , 5];
  var resultArray = array.filter(removeBlank);
  Logger.log(resultArray)
}


function removeBlank(value) {
    return value !== '';
}


別の書き方

コード.gs
function myFunction() {
  var array = [1, 2, , 3, 4, '', , , , 5];
  var resultArray = array.filter(removeBlank);
  Logger.log(resultArray)
}


function removeBlank(value) {
  if(value) {
    return value;
  }
}



Latest post

Google Apps Scriptの障害時はIssueTrackerを見てみる - Incidents for Apps Script are reported on Issue Tracker

IssueTracker > Apps Script issues https://issuetracker.google.com/savedsearches/566234 Google Apps Scriptの障害時は IssueTracker に課題が上がっていることが...