I am trying to loop through Json twice: once for parent elements and then again for more detail. (This eventually will be exported to XML) In the meantime, how do I go about adding an array to an Object? My current code doesn't create 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'];
});
});
I am trying to loop through Json twice: once for parent elements and then again for more detail. (This eventually will be exported to XML) In the meantime, how do I go about adding an array to an Object? My current code doesn't create 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'];
});
});
Share
Improve this question
edited May 31, 2012 at 18:20
MG1
asked May 31, 2012 at 17:56
MG1MG1
1,6879 gold badges33 silver badges48 bronze badges
3
- Do you want to add the object to the array or the other way round? – Bergi Commented May 31, 2012 at 18:13
- I would like to add the array XMLObjectDetail to the object XMLObject. – MG1 Commented May 31, 2012 at 18:18
- Which property name should it get in the XMLObject? – Bergi Commented May 31, 2012 at 19:54
2 Answers
Reset to default 14After you clarified your request in the ments, the solution is easy:
var XMLObject = {};
var XMLObjectDetail = [];
XMLObject["XMLObjectDetail"] = XMLObjectDetail;
you may shorten it to
var XMLObjectDetail, XMLObject = {XMLObjectDetail: XMLObjectDetail = []};
However, I need to mention some serious flaws in your code:
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 pletey inprehensible
// 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 ""
});
});
If you want to add objects for XMLObject
to array XMLObjectDetail
do like this:
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'];
});
});