Javascript 向对象添加数组
我尝试两次循环使用Json:一次用于父元素,另一次用于更详细的内容。(最终将导出为XML)同时,如何向对象添加数组?我当前的代码没有创建XMLObjectDetailJavascript 向对象添加数组,javascript,json,Javascript,Json,我尝试两次循环使用Json:一次用于父元素,另一次用于更详细的内容。(最终将导出为XML)同时,如何向对象添加数组?我当前的代码没有创建XMLObjectDetail XMLObject = {}; var XMLObjectDetail = []; $.each(data, function(index, element) { XMLObject.CardCode = element['CardCode'] XMLObject.CardName = e
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'];
});
});
如果要将
XMLObject
的对象添加到数组XMLObjectDetail
中,请执行以下操作:
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'];
});
});
在评论中澄清您的请求后,解决方案很简单:
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 ""
});
});
您想将对象添加到数组中还是相反?我想将数组XMLObjectDetail添加到对象XMLObject中。它应该在XMLObject中获得哪个属性名?很抱歉,我更正了一个错误。我正在尝试创建一个包含数组的对象。XMLObject是主对象,XMLObjectDetail是我在第二个循环中为其赋值的数组。XMLObjectDetail需要在XMLObject中。对象图是什么?或者,您的阵列是如何构造的?
数据
(在第一个$中。每个构造)是否包含多个元素
对象?element
是对象还是数组(即data
是element
数组的数组?感谢您抽出时间查看我的代码。正如您所知,我对使用Json不太熟悉。我会给您一些背景知识。此代码从csv获取数据(由数据库导出组成)。然后,它必须将其显示给用户,并允许用户进行某些更改。然后,他单击一个按钮,它将转换为XML。这是我的客户要求。我认为执行两次循环的原因是,当存在具有相同供应商id的行时,它们需要分组在一起,并且我需要显示某些值的总和。以及显示每一行(或发票)。你能推荐一种更好的方法来构造它吗?那么这两个循环是算法的哪一部分?XML序列化?我认为你应该问一个新问题,给我们一些示例数据(序列化JSON,或CSV和你的toJSON解析器),这段有问题的代码实际要实现什么,以及您遇到了哪些错误。我刚刚在这里修改了我的问题。我希望问题清楚。我非常感谢您抽出时间来帮助我。