gpt4 book ai didi

javascript - 动态访问 JSON 值

转载 作者:行者123 更新时间:2023-11-29 18:35:09 25 4
gpt4 key购买 nike

这可能是非常基础的。在下面的代码中,我已经注释了需要注意的部分。我只是不知道如何构建它,而不是 res.DATA.VALUE[i],我让它动态化并写成“res.DATA”。 + textCol + '[i]'(纯伪代码,我知道它不会工作)

    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

最佳答案

您可以使用 bracket notation通过字符串名称访问属性,如下所示:

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

关于javascript - 动态访问 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4001770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com