2012-05-31 145 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