【问题标题】:Working with JSON arrays and objects使用 JSON 数组和对象
【发布时间】:2018-04-28 22:52:13
【问题描述】:

我有json data here。我正在尝试像这样在property_more_images 中获取数据:

getItemImages: function(pid) {
    xhr.simpleCall({
        query:{ 
            com_option:"item",
            item_get_id:pid
        },
        func:'timeline'
    }, function(response) { 
           if (response.err_code === 0) { 
               console.log(response.data[0].property_more_images);

               var arr = response.data[0].property_more_images;

               notice_obj = [];
               for (var i = 0, l = arr.length; i < l; ++i) {
                   notice_obj.push(arr[i]);                               
               }

               return notice_obj.join(' ,');
           }  
       });
}

然后像这样使用getItem 函数:

getItem: function(pid) {
    var $this = $$('.time-line-content .item-content[data-id="'+ pid +'"]');
    var item = {
        id: $this.data('id'),
        nickname: $this.find('.item-header .detail .nickname').html()
    };

    item.propslider = this.getItemImages(pid);

    var output = TM.renderTplById('itemTemplate', item);

    $$('#itemContent').html(output);
}

到目前为止,我的console.log 给了我预期的 json 数据。当我在getItem 函数中调用getItemImages 函数时,我似乎没有得到任何数据。 getItemImages 函数有问题。我不知道是什么。

【问题讨论】:

  • getItemImages是异步调用,不能简单的return。您需要等待结果或在回调函数中使用结果
  • 谢谢@santi 完全忘记了我稍后会发布解决方案

标签: jquery arrays json


【解决方案1】:

【讨论】:

    猜你喜欢
    • 2022-08-22
    • 1970-01-01
    • 2018-11-16
    • 2018-07-03
    • 2017-03-27
    • 2016-02-18
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多