Return multiple values in js

With this code, you can return multiple values in JavaScript:

  1. Function:
    function newCodes(){
        var dCodes = fg.codecsCodes.rs; // Linked ICDs  
        var dCodes2 = fg.codecsCodes2.rs; //Linked CPTs       
        return {
            dCodes: dCodes, 
            dCodes2: dCodes2
        };  
    }
    
    var result = newCodes();
    alert(result.dCodes);
    alert(result.dCodes2);
    ; 

Reference link: http://stackoverflow.com/questions/2917175/return-multiple-values

Advertisements

How to call a function within $(document).ready from outside it.

With this code, you can call a ready function from outside it:

  1. Define the function on the window object to make it global from within another function scope:
    $(document).ready(function(){  
      window.lol = function(){  
        alert('lol');  
      }  
    });
    

Reference link: http://stackoverflow.com/questions/2379529/how-to-call-a-function-within-document-ready-from-outside-it

ENCODE AND DECODE JSON TEXT FROM JAVA TO JAVASCRIPT – USING MAP

JSON.simple is a simple Java toolkit for JSON. You can use JSON.simple to encode or decode JSON text.

  1. Mapping between JSON and JAVA entities:
    JSON Java
    object java.util.Map
  2. Example:
    //import org.json.simple.JSONValue;
    Encode a java.util.List in Java
    Java:

      Map<String, String> obj = new LinkedHashMap()<String, String>;
      obj.put("name","foo");
      obj.put("num",new Integer(100));
      obj.put("balance",new Double(1000.21));
      obj.put("is_vip",new Boolean(true));
      obj.put("nickname",null);
    
      String jsonText = JSONValue.toJSONString(obj);

    Decode it in JavaScript using LinkedHashMap

    JavaScript:

      var js_obj = JQuery.parseJSON(jsonText);
      // or var js_obj = JSON.parse(jsonText);
      var name = js_obj[name];
      var num = js_obj[num];
    
      // to see if key exists
      var key = "name";
      if( key in js_obj )

Reference Link: https://code.google.com/p/json-simple/
Reference Link: http://api.jquery.com/jQuery.parseJSON/
Reference Link: http://www.jquerysdk.com/api/jQuery.toJSON