开发者

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.开发者_StackOverflow社区RECORD_ID[i]);
                    thisval = res.DATA.VALUE[i]; //instead of VALUE, I want to use the textCol argument passed to this function.

..../snip


You can use bracket notation to access a property via its string name, like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜