【问题标题】:Javascript: ForEach loop correct way to access json tag through this methodJavascript:ForEach 循环正确方法通过此方法访问 json 标签
【发布时间】:2015-06-05 17:57:23
【问题描述】:

为了只获取 php 脚本的特定部分,我使用了一个带有与 id 相关的条件的 foreach,因此它给出了一个作用域结果。我现在遇到的问题是如何访问我拥有的信息......我会展示一些例子来帮助说明我的困惑

listCheck.innerHTML = "<img src = " + "./images/" + json[i].exhibits[j].exhibit_image + " Photo Cover' height='200' width='200'>";

在访问 json 之后,它会使用标签 Exhibition_image 获取适当的数据。然而,使用 ForEach 调整此方法会导致错误,因为我认为代码不知道我要求什么,我认为这是语法错误

这是我尝试过的代码...

function getNewYork()
{

myExhibitionsView = document.getElementById('exhibitioncontent');
images = document.createElement('ul');

    json.forEach( function(element) {
    if( element['exhibition_id'] == 1 ){
        console.log(element);
        for (var i = 0; i < element.length; i++) {
        for (var j = 0; j < element[i].exhibits.length; j++) {
        list = document.createElement('p');
        list.id = 'image';
        list.innerHTML = "<img src = " + "./images/" + element[i].exhibits[j].exhibit_image + " Photo Cover' height='200' width='200'>";
        console.log(list);
        myExhibitionsView.appendChild(images);
        images.appendChild(list);

        }
        }




    };
});
}

我正在尝试从这组 json 中提取展览图片 url

array("exhibition_id" => "1", "exhibition_title" => "New York, New York", "exhibition_subject" => "New York", "ticket_price" => "10",
        "exhibits" => array(
            array("exhibit_id" => "3", "exhibit_title" => "Brooklyn Bridge from City Hall Park", "exhibit_description" => "New York, June 2005", "exhibit_image" => "brooklynbridge.jpg", "photographer" => "MLG"),
            array("exhibit_id" => "6", "exhibit_title" => "Central Park, New York", "exhibit_description" => "New York, June 2005", "exhibit_image" => "centralpark.jpg", "photographer" => "MLG"),
            array("exhibit_id" => "7", "exhibit_title" => "Chrysler Building at night, New York", "exhibit_description" => "New York, July 2001", "exhibit_image" => "chrysler_building.jpg", "photographer" => "MLG")
        ),
        "locations" => array(
            array("location_id" => "1", "location_name" => "Kelvingrove Art Gallery and Museum", "location_postcode" => "G3 8AG"),
            array("location_id" => "3", "location_name" => "Walker Art Gallery", "location_postcode" => "L3 8EL"),
            array("location_id" => "5", "location_name" => "Tate Modern", "location_postcode" => "SE1 9TG")
        )
    ),

执行此操作的正确语法是什么?感谢您的帮助

【问题讨论】:

    标签: javascript php json foreach


    【解决方案1】:

    试试这个:

    http://jsfiddle.net/kzzphmrp/

    您的代码似乎存在一些问题:

    • 您似乎使用字符串作为 ID,但与 javascript 循环中的数字进行比较
    • 您有一个多余的循环,如果您正在执行 foreach,您会在回调中获得数组单个元素,因此您不需要再次循环整个数组(就像您在此处所做的那样: for (var i = 0; i &lt; element.length; i++) { ... }) 只需删除此循环并遍历元素“展览”属性:for (var j = 0; j &lt; element.exhibits.length; j++)
    • 更正了您的 HTML 输出的一些问题,我想您忘记了“alt”

    这就是你想要达到的目标?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 2012-10-17
      相关资源
      最近更新 更多