Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过动态变量访问Json值_Javascript_Json_Variables_Dynamic - Fatal编程技术网

Javascript 通过动态变量访问Json值

Javascript 通过动态变量访问Json值,javascript,json,variables,dynamic,Javascript,Json,Variables,Dynamic,好的,首先是json结构 [{ "type": "button", "name": "Off", "tabname": "1", "image_file": "path\\Off.gif" }, { "type": "button", "name": "Off1", "tabname": "2", "image_file": "path\\Off1.gif", "image_file_1": "path\\On1.gif

好的,首先是json结构

[{
    "type": "button",
    "name": "Off",
    "tabname": "1",
    "image_file": "path\\Off.gif"
  }, {
    "type": "button",
    "name": "Off1",
    "tabname": "2",
    "image_file": "path\\Off1.gif",
    "image_file_1": "path\\On1.gif"
  }, {
    "type": "button",
    "name": "Off2",
    "tabname": "3",
    "image_file": "path\\Off2.gif",
    "image_file_1": "path\\On2.gif",
    "image_file_2": "path\\half.gif"
  }
]
image\u file字段可以有多个条目(即image\u file、image\u file\u 1、image\u file\u 2等),我如何在循环中动态访问它

当前不工作的代码(只是相关的东西)

第一次迭代工作正常(即
if(imageIndex==1)
位),但我为测试值而输入的
else
子句警报始终返回未定义的值


如果您跳过
image\u file\u 1
,直接从
image\u file
转到
image\u file\u 2

尝试在0处启动imageIndex,并将0映射到
image\u文件

imageIndex = 0;
continueLoop = true;
while(continueLoop) {
  if(imageIndex == 0) {
    images.push(data[i].image_file);
  }
  // rest of your code unchanged

您正在跳过
image\u文件\u 1
,直接从
image\u文件
转到
image\u文件\u 2

尝试在0处启动imageIndex,并将0映射到
image\u文件

imageIndex = 0;
continueLoop = true;
while(continueLoop) {
  if(imageIndex == 0) {
    images.push(data[i].image_file);
  }
  // rest of your code unchanged

据我所知,您的代码中包含了所有的
image\u文件
属性,甚至是带有数字的属性:


据我所知,您的代码中包含了所有的
image\u文件
属性,甚至是带有数字的属性:


在这种情况下,最好的选择是将图像_文件设置为JSON格式的数组

[{
   ...
  }, {
    "type": "button",
    "name": "Off2",
    "tabname": "3",
    "image_files": ["path\\Off2.gif", "path\\On2.gif","path\\half.gif"]
  }
]
然后你可以优雅地重复这些

但由于我们都知道必须使用其他人的JSON的痛苦,因此我提取这些数据的方法如下:

images = []
$.each(data, function(i, item) {
    imageIndex = 0
    while ( true ) {
        image = item["image_file" + ( imageIndex === 0 ? "" : "_" + imageIndex )]
        if ( typeof image === 'undefined' ) {
            break
        }
        images.push(image)
        imageIndex += 1
    }
})

在这种情况下,最好的选择是将图像_文件设置为JSON格式的数组

[{
   ...
  }, {
    "type": "button",
    "name": "Off2",
    "tabname": "3",
    "image_files": ["path\\Off2.gif", "path\\On2.gif","path\\half.gif"]
  }
]
然后你可以优雅地重复这些

但由于我们都知道必须使用其他人的JSON的痛苦,因此我提取这些数据的方法如下:

images = []
$.each(data, function(i, item) {
    imageIndex = 0
    while ( true ) {
        image = item["image_file" + ( imageIndex === 0 ? "" : "_" + imageIndex )]
        if ( typeof image === 'undefined' ) {
            break
        }
        images.push(image)
        imageIndex += 1
    }
})

是的,谢谢你,很高兴我只是个傻瓜,尽管我会选择Joseph the dreamers code,因为它为纽约的需求提供了最优雅的解决方案-感谢所有的想法是的,谢谢你,很高兴我只是个傻瓜,虽然我会选择Joseph the dreamers code,因为它为纽约的需求提供了最优雅的解决方案-感谢所有周到的非常优雅的解决方案,即使我的问题是由jcsanyi强调的简单疏忽造成的,但这对我来说是一个更好的方法,目前非常优雅的解决方案,即使我的问题是由jcsanyi强调的简单疏忽引起的,但这是目前对我来说更好的方法