javascript - 如何在foreach循环内获取jquery foreach中的值

标签 javascript jquery

我有一个包含一个数组的 json 响应,在该数组内我还有一个数组。

我的 AJAX:

$.ajax({
  type: "POST",
  data: { city: city, priceRange: range },
  url: "rentpriceRangeFilter.php",
  success: function(data) {
    var res = jQuery.parseJSON(data); // convert the json
    console.log(res);
    if (res['status'] == 1) {
      var htmlString = '';
      $.each(res['data'], function(key, value) {
        console.log(key + ' is ' + value.house);
      });
    }
  },
});

rentpriceRangeFilter.php(我返回 json 响应)

echo json_encode(array("status"=>"1","response"=>"success","data"=>$mainArray));

我的 JSON 响应如下:

{
  "status": "1",
  "response": "success",
  "data": [{
    "id": "2",
    "house": "1 BHK Apartment for Lease",
    "rentLease_amount": "6000000",
    "furnished_type": "Semi-Furnished",
    "CreatedOn": null,
    "Rent": "60 Lac",
    "multipleImages": [{
      "imageId": "3",
      "rentImageId": "2",
      "rentImageName": "2cf011438e6cd43c6bfaa6cac653d86e.jpg"
    }, {
      "imageId": "4",
      "rentImageId": "2",
      "rentImageName": "e2c5be420130370c5118120a1bc749c6.jpg"
    }]
  }, {
    "id": "1",
    "house": "1 BHK Aparatment for Marathahalli",
    "rentLease_amount": "500000",
    "furnished_type": "Fully-Furnished",
    "CreatedOn": null,
    "Rent": "5 Lac",
    "multipleImages": [{
      "imageId": "1",
      "rentImageId": "1",
      "rentImageName": "7d2905c30ab211732b97dbf165c75526.jpg"
    }, {
      "imageId": "2",
      "rentImageId": "1",
      "rentImageName": "bcf6cb343aaa2cc8e50ff52baa062bcc.jpg"
    }]
  }]
}

现在我想显示house名称和multipleImages值,但是在我的ajax页面中我只能获取房屋名称,我不知道如何显示 >多个图像

最佳答案

您可以在 multipleImages 上使用另一个循环,如下所示:

$.ajax({
    type: "POST",
    data: {city:city,priceRange:range},
    url: "rentpriceRangeFilter.php",
    success:function(data){
        var res=jQuery.parseJSON(data);// convert the json
        console.log(res);

        if(res['status']==1){
            var htmlString='';
            $.each( res['data'], function( key, value ) {
                console.log(key + ' is ' + value.house);
                $.each( value.multipleImages, function( key2, image ) {             
                    console.log('imageId of ' + key2 + ' is ' + image.imageId);
                    console.log('rentImageId of ' + key2 + ' is ' + image.rentImageId);
                    console.log('rentImageName of ' + key2 + ' is ' + image.rentImageName);
                });
            });
        }
    },
});

关于javascript - 如何在foreach循环内获取jquery foreach中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44708470/

相关文章:

javascript - CSS 在 Firefox 中不工作(或 HTML 显示为简单文本)

javascript - Jquery递减点击

javascript - jQuery 插件或带有 css 的 jQuery 用于弹出窗口

javascript - 使用 jQuery 按钮在文本和编辑表单之间来回切换

javascript - 到达 polymer slideToggle()函数,但没有结果

javascript - 用户滚动时创建一个javascript弹出框

javascript - 如何让 GTM 标签等待加载一些 jQuery?

jquery - 缩小窗口大小时,水平 Accordion div 会下降

javascript - JS Flot 图表 : 2 y-axes with the same data. 一张是数字数据,一张是百分比

javascript - cookie 的设置值 - 名称中的冒号