-1

I have an object in javascript for settings for dataTables.

var dataTableSettings = {
  "iDisplayLength": 25,
  "bLengthChange": false,
  "bSortClasses": false,
};

I then have an if statement to add another option to the object:

if (last_location) {
  dataTableSettings.push(
    "oSearch": {"sSearch": last_location}
  );
}

I know that doesn't work because push() doesn't work on objects, how do I add to the object options list?

Lovelock
  • 7,689
  • 19
  • 86
  • 186

3 Answers3

2
dataTableSettings.oSearch = { "sSearch": lastLocation };

Or

dataTableSettings['oSearch'] = { "sSearch": lastLocation };
putvande
  • 15,068
  • 3
  • 34
  • 50
NMunro
  • 890
  • 5
  • 20
1

You have to do it this way:

if (last_location) {
  dataTableSettings.oSearch = {"sSearch": last_location};
}

OR

if (last_location) {
      dataTableSettings["oSearch"] = {"sSearch": last_location};
}
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
0

just do this :

dataTableSettings.oSearch = {"sSearch": last_location}

or this (both are equivalent ):

dataTableSettings['oSearch'] = {"sSearch": last_location}
Remy Grandin
  • 1,638
  • 1
  • 14
  • 34