简体   繁体   English

在JavaScript中引用JSONObject时,如何使用变量(而不是名称本身)引用它?

[英]When referring to a JSONObject in javascript, how do I refer to it using a variable (rather than the name itself)?

I looked up all the other StackOverflow posts for this, but none seemed to work. 我为此查找了所有其他StackOverflow帖子,但似乎都没有用。 I'll post my code below: 我将在下面发布我的代码:

$.getJSON('bugs.json', function (data) {
    for ( var observer = 1; observer <= data.numObservers; observer++) {
        var observerName = "num" + observer;

        var specs = ['congestion', 'bloodflow'];
        for ( var spec in specs) {
            var tableCode = "<br><table id=\"" + observer + specs[spec] + "\" > <thead> <tr> <th>" + specs[spec] + "</th> <th id=\"value\">Value</th> <th id=\"time\">Time</th></tr>";
            $('body').append(tableCode);
            var observerObj = data[observerName] + " kaka " + data.num1.congestion.values[1];

            var selectedSpec = specs[spec];
            for (var i = 0; i < observerObj[selectedSpec].values.length; i++) {
                tr = $('<tr/>');
                tr.append ("<td> Name </td>");
                tr.append("<td>" + data[observerName][spec].values[i] + "</td>");
                tr.append("<td>" + data[observerName][spec].times[i] + "</td>");
                $('#'+observer+spec).append(tr);                
            }
        }
    }
});

When using the browser console log, I get an error that observerObj[selectedSpec] doesn't work. 使用浏览器控制台日志时,出现一个错误,指示observerObj[selectedSpec]不起作用。

Thanks in advance for your help. 在此先感谢您的帮助。

Edit : the exact console error is: 编辑 :确切的控制台错误是:

TypeError: observerObj[selectedSpec] is undefined

var observerObj = data[observerName] + " kaka " + data.num1.congestion.values[1];

If I am reading this correctly, observerObj is a string - not an array or object. 如果我正确地阅读了此内容,则observerObj是一个字符串,而不是数组或对象。 So observerObj[selectedSpec] would not exist. 因此,observerObj [selectedSpec]将不存在。

You're trying to iterate over an array as an object. 您试图将数组作为对象进行迭代。

var specs = ['congestion', 'bloodflow'];
for ( var spec in specs) {

This is trying to iterate for every property on specs not each object inside of it. 这试图遍历specs每个property ,而不是其中的每个对象。 You want to do something like this... 你想做这样的事情...

var specs = ['congestion', 'bloodflow'];
for ( var i = 0; i < specs.length; i++) {

Then access each item via specs[i] 然后通过specs[i]访问每个项目

Hope this helps! 希望这可以帮助!

MDN Article about for ... in should help too. 关于for ... in MDN文章也应该有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 php/html 中的程序名称作为变量传递给 javascript,而不是在“$.get”语句中对其进行硬编码 - How do I pass a program name from php/html, as a variable to javascript rather than hard coding it in the ' $.get' statement 如何使用 JavaScript 变量在不同条件下引用 2 个以上的值? - How do I use a JavaScript variable to refer to more than 2 values in different conditions? 如何使用文件名称而不是使用.htaccess扩展名缓存文件 - How do I cache a file by it's name rather than extension using .htaccess 如何发送 json object javascript 可以理解为数组而不是使用 ZE1E1D3D40573127E9EE0480CAFZ12 和管道工的字符串? - How do I send json object that javascript can understand as array rather than string using R and plumber? 如何使变量引用其他变量? [Javascript] - How do I make a variable refer to other variables? [Javascript] 如何使用包含变量名称的字符串来引用变量? - How can I refer to a variable using a string containing its name? 如何将箭头键传递给Javascript,而不是移动WebView本身? - How to pass arrow key to Javascript rather than moving WebView itself? Javascript mongoDB采用变量名称而不是变量值 - Javascript mongoDB takes variable name rather than value of the variable javascript - 如何引用对象本身? - javascript - how to refer object itself? 在使用setTimeout时,如何确保Javascript的“ this”将引用该对象? - How do I ensure that Javascript's “this” will refer to the object when using setTimeout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM