简体   繁体   中英

Access JSON value dynamically

This is probably unbelievably basic. In the code below, I have annotated the part that needs attention. I just do not know how to structure it if, instead of res.DATA.VALUE[i], I make it dynamic and write 'res.DATA.' + textCol + '[i]' (pure pseudocode, I realise it won't work)

    function loadSelect(entity,textCol,retField,method) {
        var thisid;
        var thisval;    
        var textCol = textCol.toUpperCase();

        $.getJSON("/cfcs/system.cfc?method=" + method + "&returnformat=json",{},function(res,code) {
            if(res && res.ROWCOUNT > 0)
            {
                for(var i=0; i<res.ROWCOUNT; i++){ 
                    thisid = parseInt(res.DATA.RECORD_ID[i]);
                    thisval = res.DATA.VALUE[i]; //instead of VALUE, I want to use the textCol argument passed to this function.

..../snip

您可以使用方括号表示法通过其字符串名称访问属性,如下所示:

thisval = res.DATA[textCol][i];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM