2012-05-31 187 views
9

我試圖通過Json循環兩次:一次爲父元素,然後再次獲取更多詳細信息。 (這最終會導出到XML)同時,我該如何去添加一個數組到一個對象?如果您想爲XMLObject對象添加到陣列XMLObjectDetail做這樣我當前的代碼不會產生XMLObjectDetail將數組添加到對象

XMLObject = {}; 
var XMLObjectDetail = []; 

$.each(data, function(index, element) { 
     XMLObject.CardCode = element['CardCode'] 
     XMLObject.CardName = element['CardName']; 
     console.log(XMLObject); 

    $.each(element, function(key, value) { 
     XMLObject[[XMLObjectDetail.InvPayAmnt]] = value['InvPayAmnt']; 
     }); 
    }); 
+0

你要的對象添加到陣列或其他方式輪? – Bergi

+0

我想將XMLObjectDetail數組添加到對象XMLObject中。 – MG1

+0

它應該在XMLObject中獲得哪個屬性名稱? – Bergi

回答

11

後您澄清在評論你的要求,該解決方案易於:

var XMLObject = {}; 
var XMLObjectDetail = []; 
XMLObject["XMLObjectDetail"] = XMLObjectDetail; 

您可以縮短到

var XMLObjectDetail, XMLObject = {XMLObjectDetail: XMLObjectDetail = []}; 

不過,我需要提及的代碼中的一些嚴重的缺陷:

XMLObject = {}; // no var keyword: the variable will be global 
var XMLObjectDetail = []; 

$.each(data, function(index, element) { 
    // I don't know how your data object/array looks like, but your code will be 
    // executed many times 
    // For each element, you will overwrite the properties 
    XMLObject.CardCode = element['CardCode'] // missing semicolon 
    XMLObject.CardName = element['CardName']; 
    // so that the final XMLObject will only contain cardcode and -name of the last one 
    // It will depend on your console whether you see different objects 
    // or the same object reference all the time 
    console.log(XMLObject); 

    // This part is completey incomprehensible 
    // you now loop over the properties of the current element, e.g. CardCode 
    $.each(element, function(key, value) { 
     // and again you only overwrite the same property all the time 
     XMLObject[[XMLObjectDetail.InvPayAmnt]] = value['InvPayAmnt']; 
     // but wait: The property name you try to set is very, um, interesting. 
     // first, XMLObjectDetail is still an (empty) Array and has 
     // no 'InvPayAmnt' property - leads to a undefined 
     // then, you build an Array with that [undefined] value as the only item 
     // OMG, an array? JavaScript does only allow strings as property names, 
     // so the array will be converted to a string - resulting to the empty string "" 
    }); 
}); 
+0

謝謝你花時間看看我的代碼。你可以告訴我,與Json合作是新的。會給你一些背景,這些代碼從一個csv(由一個數據庫導出的數據)中獲取數據,然後顯示給用戶並允許他做一些修改,然後,他點擊一個按鈕,然後將它轉換爲XML,這是我的客戶要求 – MG1

+0

我認爲要循環兩次的原因是因爲當有一些具有相同供應商ID的行時,他們需要組合在一起,我需要以顯示某些值的總和以及顯示每個單獨的行(或發票),你能推薦一個更好的方法來構造這個結構嗎? – MG1

+0

那麼你的算法的哪一部分是這兩個循環?XML序列化?我認爲你應該問一個新的問題,給我們一些示例數據(序列化的JSON或th e CSV和toJSON解析器),您實際嘗試使用有問題的代碼片段以及遇到的錯誤來實現的目標。 – Bergi

3

var XMLObject, XMLObjectDetail = []; 

$.each(data, function(index, element) { 
     XMLObject=new Object(); //or XMLObject = {}; 
     XMLObject.CardCode = element['CardCode'] 
     XMLObject.CardName = element['CardName']; 
     console.log(XMLObject); 

     XMLObjectDetail.push(XMLObject);//ADDED OBJECT TO ARRAY 

     //DON'T KNOW WHAT ARE YOU TRYING TO DO HERE? 
     $.each(element, function(key, value) { 
      XMLObjectDetail[[XMLObjectDetail.InvPayAmnt]] = value['InvPayAmnt']; 
     }); 
    }); 
+0

對不起,我糾正了一個錯誤。我正在試圖創建一個內部數組的對象。 XMLObject是主要對象,XMLObjectDetail是我在第二個循環中爲其分配值的數組。 XMLObjectDetail需要在XMLObject中。 – MG1

+0

你的對象圖是什麼?或者你的陣列結構如何? 'data'(在第一個'.each'構造中)是否包含多個'element'對象?元素是一個對象還是一個數組(data是數組元素的數組)? – TheVillageIdiot