如何继续下一个项目而不是在forEach中未定义处停止

时间:2019-05-07 14:15:35

标签: jquery json foreach

我正在使用forEach函数从json数据生成多个div。当我的json数据中只有几个测试项目时,它工作正常。现在json最多包含50个以上的项目集,未定义零将取消该功能。 +e.portal.fields[0].xxxx+的任何原因。我一步一步地删除了+e.portal.fields[0].xxxx+,以确定是否是问题所在,但是我必须删除对fields[0]的所有引用才能完成该功能。请注意,对fields[1]的引用可以正常工作。

var lists = "";
dataJ.items.forEach(function(e){
var jobcard = '<article id="listing_'+e.adId+'" attrlocation="'+e.portal.fields[1].valueId+'" title="'+e.title+'" class="col-12 mb-3 all_i all_r all_l '+e.portal.fields[0].valueId+' '+e.portal.fields[0].fields[0].valueId+' '+e.portal.fields[1].value+'  '+e.portal.fields[1].valueId+'"><div class="container article align-items-stretch bg-white"><div class="row no-gutters"><div class="col-lg-10 col-sm-12 job-detail-contents bg-white p-3"><div class="row"><div class="col-lg-7 col-sm-12 job-title text-capitalize"><h2>'+e.title+'</h2></div><div class="col-lg-5 col-sm-12 job-mini-details time-stmp">posted&nbsp;<time class="timeago" datetime="'+e.postedAt+'"> '+e.postedAt+'</time></div></div><div class="row" ><div class="col-lg-7 col-xs-4"><span style="color: #0c53a0; font-size: .85em; font-weight: 600; padding-right: 10px;text-transform: uppercase; letter-spacing: .12em;">Summary:</span><p class="job-details job_spec_summary" style="font-size: .85em;">'+e.summary+'</p></div><div class="col-lg-5 col-xs-8 job-mini-details"><div class="row"><div class="col-12"><i class="fa fa-map-marker-alt fa-sm pr-1 job_spec" style="margin-top: -.1em; color: #0c53a0;"></i><span>'+e.portal.fields[1].value+'</span></div><div class="col-12"><i class="fas fa-address-card pr-1" style="color: #0c53a0;"></i><span>'+e.portal.fields[0].value+'</span></div><div class="col-12"><i class="fas fa-building pr-1" style="color: #0c53a0;"></i><span>'+e.portal.fields[0].fields[0].value+'</span></div></div></div></div></div><div class="col-lg-2 col-sm-12 bg-transparent mb-sm-2 text-right"><button id="link-to-'+e.adId+'" href="#details-'+e.adId+'" class="btn btn-primary animated p-3 mt-5 mb-5 mr-1 changing-btn" data-hover="Check it Out" data-active="Please Wait..."><span class="d-inline-block text-white text-uppercase font-weight-bold pl-2"><i class="far fa-eye fa-lg text-white d-inline-block pr-2 "></i>details</span></button></div></div></div></article>';
lists+=jobcard;
});
$('#flatdiv').append(lists);

这是json的布局:

    {  
   adId:75480,
   title:"some title",
   reference:"219109",
   summary:"some summary",
   bulletPoints:[  
      "something",
      "something",
      "something"
   ],
   portal:{  
      fields:[  
         {  
            fieldId:something,
            fieldName:"something",
            valueId:something,
            value:"something",
            fields:[  
               {  
                  fieldId:something,
                  fieldName:"something",
                  valueId:something,
                  value:"something"
               }
            ]
         },
         {  
            fieldId:something,
            fieldName:"something",
            valueId:something,
            value:"something"
         },
         {  
            fieldId:something,
            fieldName:"something",
            valueId:something,
            value:"something"
         }
      ]
   },
   description:"something"
},
....

其中一个项目集可能根本没有该字段,但是仔细检查json数据的页面和页面以发现哪一个似乎很痛苦,而我却无法发现有问题的项目集。 / p>

我该如何对我的代码做些什么,以简单地跳过导致错误的代码并继续到需要创建的下一个div?

已更新:

dataJ.items.forEach(function(e){

if ($(e.portal.fields[0].fields[0].valueId).length > 0){
    var jobcard = '<article id="listing_'+e.adId+'" attrlocation="'+e.portal.fields[1].valueId+'" title="'+e.title+'" class="col-12 mb-3 all_i all_r all_l '+e.portal.fields[0].valueId+' '+e.portal.fields[0].fields[0].valueId+' '+e.portal.fields[1].value+'  '+e.portal.fields[1].valueId+'"><div class="container article align-items-stretch bg-white"><div class="row no-gutters"><div class="col-lg-10 col-sm-12 job-detail-contents bg-white p-3"><div class="row"><div class="col-lg-7 col-sm-12 job-title text-capitalize"><h2>'+e.title+'</h2></div><div class="col-lg-5 col-sm-12 job-mini-details time-stmp">posted&nbsp;<time class="timeago" datetime="'+e.postedAt+'"> '+e.postedAt+'</time></div></div><div class="row" ><div class="col-lg-7 col-xs-4"><span style="color: #0c53a0; font-size: .85em; font-weight: 600; padding-right: 10px;text-transform: uppercase; letter-spacing: .12em;">Summary:</span><p class="job-details job_spec_summary" style="font-size: .85em;">'+e.summary+'</p></div><div class="col-lg-5 col-xs-8 job-mini-details"><div class="row"><div class="col-12"><i class="fa fa-map-marker-alt fa-sm pr-1 job_spec" style="margin-top: -.1em; color: #0c53a0;"></i><span>'+e.portal.fields[1].value+'</span></div><div class="col-12"><i class="fas fa-address-card pr-1" style="color: #0c53a0;"></i><span>'+e.portal.fields[0].value+'</span></div><div class="col-12"><i class="fas fa-building pr-1" style="color: #0c53a0;"></i><span>'+e.portal.fields[0].fields[0].value+'</span></div></div></div></div></div><div class="col-lg-2 col-sm-12 bg-transparent mb-sm-2 text-right"><button id="link-to-'+e.adId+'" href="#details-'+e.adId+'" class="btn btn-primary animated p-3 mt-5 mb-5 mr-1 changing-btn" data-hover="Check it Out" data-active="Please Wait..."><span class="d-inline-block text-white text-uppercase font-weight-bold pl-2"><i class="far fa-eye fa-lg text-white d-inline-block pr-2 "></i>details</span></button></div></div></div></article>';
    console.log(e.adId);                
} else {
return
};

1 个答案:

答案 0 :(得分:0)

上面编辑的代码解决了此问题。