【问题标题】:Accessing Json value by dynamic variable通过动态变量访问 Json 值
【发布时间】:2013-06-04 10:10:06
【问题描述】:

好的,首先是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_file 字段可以有多个条目(即 image_file、image_file_1、image_file_2 等),我如何在循环中动态访问?

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

$.each(data, function (i, item) {
  var images = [];
  imageIndex = 1;
  continueLoop = true;
  while(continueLoop) {
    if(imageIndex == 1) {
      images.push(data[i].image_file);
    }
    else {
      var testVal = 'image_file_' + imageIndex;
      alert(data[i][testVal]);
      if(data[i][testVal] === undefined) {
        continueLoop = false;
      }
      else {
        images.push(data[i][testVal]);

      }
    }
    imageIndex++;
  }
});

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

任何帮助将不胜感激

【问题讨论】:

    标签: javascript json variables dynamic


    【解决方案1】:

    据我所知,您将获得所有 image_file 属性,即使是带有数字的属性。 You can easily do it with this:

    var images = [];
    
    //iterate through the array
    $.each(data, function (i, item) {
    
      //iterate through each property
      $.each(item, function (key, value) {
    
        //if the property starts with image_file, push into the array
        if (key.indexOf('image_file') === 0) images.push(value);
      });
    });
    
    console.log(images); // ["path\\Off.gif","path\\Off1.gif","path\\On1.gif","path\\Off2.gif","path\\On2.gif","path\\half.gif"]
    

    【讨论】:

    • 非常优雅的解决方案,即使我的问题是由 jcsanyi 强调的简单疏忽引起的,这对我来说是目前更好的方法
    【解决方案2】:

    您正在跳过image_file_1,直接从image_file 转到image_file_2

    尝试从 0 开始 imageIndex,并将 0 映射到 image_file

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

    【讨论】:

    • 是的,谢谢你,很高兴只是我是个笨蛋,虽然我不喜欢 Joseph 的梦想家代码,因为它为你的需求提供了最优雅的解决方案——尽管如此
    【解决方案3】:

    在这种情况下,最好的选择是将 image_files 设为 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
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 2022-04-26
      • 2010-10-15
      • 2014-10-11
      • 1970-01-01
      相关资源
      最近更新 更多