2012-02-25 36 views
0

所以我循環了一組JSON數組,然後遍歷每個單獨的數組。如果聲明中斷.push方法

如果我刪除了兩個if語句它會通過兩組數據環和附加的「目標」與兩個「數據」組,但像現在是,它只會通過第一回路的每個追加items.join 。

案件的東西是一個if循環首先,認爲它會很重要,但沒有。

奇怪的是,consoleOut(如console.log)打印我的東西,所以它實際上是通過JSON集內的兩個數組循環。

$.each(data, function(keys, vals) 
    { 

    var items = []; 


    $.each(data[keys], function(key, val) 
    { 

     var currString = JSON.stringify(val); 
     switch (key) 
     { 
     case "ID":    
      consoleOut("ID: "+currString+" loaded"); 
     break;          

     case "parentIDs": 

      consoleOut("parentIDs: "+currString); 
     break; 
     case "UID": 
      consoleOut("UID: "+currString); 
      UID = unquote(currString); 
     break; 
     case "title": 
      consoleOut("Title: "+currString); 
      items.push('<H1 class="' + key + '">' + unquote(currString) + '</H1>'); 
     break; 
     case "img": 
      consoleOut("IMG: "+currString); 

      if ($(currString).val() !== '') 
      { 
      items.push('<IMG class="' + key + '" src='+currString+'></IMG>');    
      }; 
     break; 
     case "text": 
      consoleOut("TXT: "+currString); 

      if ($(currString).val() !== '') 
      { 
      items.push('<P class="' + key + '">'+currString+'</P>'); 

      }; 
     break; 
     } 
     }); 
     consoleOut("---------"); 

     $('<DIV/>', { 
     'id': UID, 
     html: items.join('') 
     }).appendTo(target); 
    } 
) 

我想知道如何在if語句打破我追加到主「項目」陣..

(當我不字符串化之初它將循環的VAL,但是這因爲if語句是那麼真實,但停止假時工作..所以,也許這是更好地修改我的if語句對於這一點,但我仍然困惑的是在.push停止如何工作的?)

+0

嘗試改變'!==''來!=',看看它是否有效! – linuxeasy 2012-02-25 15:19:16

+0

不,試過 – TrySpace 2012-02-25 15:24:13

+0

是否可以爲你設置一個[jsfiddle](http://jsfiddle.net) – Henesnarfel 2012-02-25 15:26:40

回答

1
$(currString).val() 

它表示你正在製作一個元素並查看該元素的值屬性。這是你真正想要的嗎?我不認爲圖像或文字有價值。

我想你可能需要使用長度或HTML()

[編輯]看你的對象,你只是要檢查的currString

if(currString && currString.length>0) { 
+0

它很難告訴我們什麼時候我們沒有JSON。 – Chad 2012-02-25 15:29:09

+0

[{「ID」:「1」,「parentIDs」:null,「UID」:「home」,「title」:「Home」,「text」:「<3 here」,「img」:「/ favicon .ICO」, 「URL」: 「家」, 「CAT_ID」: 「1」, 「的view_id」: 「」, 「css_id」:空},{ 「ID」: 「4」, 「parentIDs」: 「家」 ,「UID」:「home_info」,「title」:「關於首頁的信息」,「文本」:「關於首頁的信息」,「img」:「」,「url」:「」,「cat_id」:「 「view_id」:「」,「css_id」:null}] – TrySpace 2012-02-25 15:44:33

+0

我試過.length!= 0,無濟於事。我只是想在「」爲空時跳過.push ... – TrySpace 2012-02-25 15:45:30

0

這不是JSON的lenght,它是一個數組。

[{"ID":"1", 
    "parentIDs":null, 
    "UID":"home", 
    "title":"Home", 
    "text":"<3 here", 
    "img":"/favicon.ico", 
    "url":"home", 
    "cat_id":"1", 
    "view_id":"", 
    "css_id":null}‌​, 
{"ID":"4", 
    "parentIDs":"home", 
    "UID":"home_info", 
    "title":"Info about Home", 
    "text":"Info about Home", 
    "img":"", 
    "url":"", 
    "cat_id":"", 
    "view_id":"", 
    "css_id":null} 
] 

你確定你的輸入不會是這個樣子(即是JSON)?

{ "data" : [{"ID":"1", 
      "parentIDs":null, 
      "UID":"home", 
      "title":"Home", 
      "text":"<3 here", 
      "img":"/favicon.ico", 
      "url":"home", 
      "cat_id":"1", 
      "view_id":"", 
      "css_id":null}‌​, 
      {"ID":"4", 
      "parentIDs":"home", 
      "UID":"home_info", 
      "title":"Info about Home", 
      "text":"Info about Home", 
      "img":"", 
      "url":"", 
      "cat_id":"", 
      "view_id":"", 
      "css_id":null} 
      ] }