-1

How to call all options in only one mode? Example: hide("house, building, clothes") or hide("house","building","clothes")... Is possible?

// == shows all markers of a particular category and ensures the checkbox is checked
function show(category) {
    for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == category) {
            markers[i].setVisible(true);
        }
    }
}
// hides all markers of a particular category and ensures the checkbox is cleared
function hide(category) {
    for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == category) {
            markers[i].setVisible(false);
        }
    }
}
// show or hide the categories initially
hide("house");
hide("building");
hide("clothes");

Thanks all :)

GTA Crazy
  • 5
  • 1
  • See http://stackoverflow.com/questions/1959040/is-it-possible-to-send-a-variable-number-of-arguments-to-a-javascript-function – David Maust Nov 21 '15 at 18:22

2 Answers2

1

You can use javascript object:

toggleShowHide({"house":true,"building":false,"clothes":true});
function toggleShowHide(options) {
  for(key in options)
  {
    if (options.hasOwnProperty(key)) {
      for (var i=0; i<locations.length; i++) {
          if (locations[i][2] == key) {
             markers[i].setVisible(options[key]);
          }
        }
      }
  }

}

See description for hasOwnProperty

Shhade Slman
  • 263
  • 2
  • 10
0

use arguments object

function show() {
    for(var j = 0; j<arguments.length; j++){
      for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == arguments[j]) {
            markers[i].setVisible(true);
        }
      }
    }
}
// hides all markers of a particular category and ensures the checkbox is cleared
function hide() {
    for(var j = 0; j<arguments.length; j++){
      for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == arguments[j]) {
            markers[i].setVisible(false);
        }
      }
}
// show or hide the categories initially
hide("house", "building", "clothes");
Azad
  • 5,144
  • 4
  • 28
  • 56