0

I am working for some common method in javascript, for that i have to call array dynamically.

 var ddlText, ddlValue, ddl, lblMesg, ddlTextCacheList_Designation, ddlValueCacheList_Designation, ddlTextCacheList_Scale, ddlValueCacheList_Scale;
 function cacheDes() {
      
        var listDes = document.getElementById("<%=List_Designation.ClientID %>");
        ddlTextCacheList_Designation = new Array();
        ddlValueCacheList_Designation = new Array();
        for (var i = 0; i < listDes.options.length; i++) {
            ddlTextCacheList_Designation[ddlTextCacheList_Designation.length] = listDes.options[i].text;
            ddlValueCacheList_Designation[ddlValueCacheList_Designation.length] = listDes.options[i].value;

        }

        
    }


    function cacheScale() {
        var listScale = document.getElementById("<%=List_Scale.ClientID %>");
        ddlTextCacheList_Scale = new Array();
        ddlValueCacheList_Scale = new Array();
        for (var i = 0; i < listScale.options.length; i++) {
            ddlTextCacheList_Scale[ddlTextCacheList_Scale.length] = listScale.options[i].text;
            ddlValueCacheList_Scale[ddlValueCacheList_Scale.length] = listScale.options[i].value;

        }


    }

    window.onload = function () {
        cacheDes();
        cacheScale();
    };

I want to call array ddlTextCacheList_Scale or ddlTextCacheList_Designation for same method as we know 'ddlTextCacheList_' is common only we need to put 'Scale' or 'Designation' dynamicaaly by passing parameter.

Add:
I get some errors:
enter image description here

9
  • they are global both, so you can use window[arrName] syntax Commented Dec 29, 2014 at 11:40
  • but better to use one object with both arrays: {texts: [], values: []} Commented Dec 29, 2014 at 11:42
  • i want to pass just 'Designation' or 'scale' through parameter and that name should be concat in 'ddlTextCacheList_' there fore appropriate array must be call. i.e. if i say Designation than ddlTextCacheList_Designation and ddlValueCacheList_Designation must call Commented Dec 29, 2014 at 11:42
  • 1
    window['ddlValueCacheList_'+param] Commented Dec 29, 2014 at 11:43
  • but better move out your business logic from variable names - make object with keys: {Scale: [{text: '', value: 1}, {text: '', value: 2}]} Commented Dec 29, 2014 at 11:45

1 Answer 1

1

I suggest you to improve your cache to store all in one object to easy access...
For example:

var CacheStorage = {};
function cache(key, list) {
  CacheStorage[key] = list.map(function(option){
    return {text: option.text, value: option.value};
  });
}
function del(key, index) {
  CacheStorage[key].splice(index, 1);
}

cache('Scale', getElementByID('...').options);
cache('Designation', getElementByID('...').options);
del('Designation', 0);
Sign up to request clarification or add additional context in comments.

9 Comments

just want to ask... is there need to call cache on window.onload event ?
It depends... it need, if your lists can be not loaded in the call time.
yes i need. can you please tell me how to do that in this scenario
i am asking because this cache function become parametrized now. So i dont have any idea how to call this onload
no worries i did it. But its an exception 'JavaScript runtime error: Object doesn't support property or method 'map''
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.