После выяснены ваш запрос в комментарии, решение легко :
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 ""
});
});
Вы хотите добавить объект в массив или наоборот? – Bergi
Я хотел бы добавить массив XMLObjectDetail к объекту XMLObject. – MG1
Какое имя свойства должно быть в XMLObject? – Bergi