kamaci
kamaci

Reputation: 75127

How to pass an array of arrays into a function at JavaScript?

How can I define an array of arrays and pass that variable into a function so I can manipulate it at JavaScript?

As like:

JSP side:

object.method({ {"#id1",2}, {"#id2",3}...});
...

JS side:

var object= {
defaults: {
  idSelector: "#id1"
},
method: function(options) {
  if (options && !jQuery.isEmptyObject(options))
     $.extend(this.defaults, options);
     var that = this;
     var opts = that.defaults;
     //Try to reach every array step by step?
      });
   }
}

Upvotes: 0

Views: 402

Answers (3)

kamaci
kamaci

Reputation: 75127

The functions' variable that will get the values should be like that(JSON format will be used):

defaults: [{
  idSelector: '',
  faqId: '' 
}]

Upvotes: 0

bluefoot
bluefoot

Reputation: 10580

Here's one of the ways to do that:

  1. Your servlet can return a text, representing a json dictionary. Here's the documentation of a JSON API http://www.json.org/java/
  2. Your javascript client code can fetch this json dictionary, something like:

    $.getJSON('ajax/test.json', function(data) {
    var items = [];
    
    $.each(data, function(key, val) {
        items.push('<li id="' + key + '">' + val + '</li>');
    });
    
    });
    

now items points to a bunch of <li> with your results

Upvotes: 0

DhruvPathak
DhruvPathak

Reputation: 43235

use json data format . Convert your object into json string in your JSP page. Parse that JSON string in your javascript.

Upvotes: 1

Related Questions