我可以使用push({})来创建我的javascript数组,但是我的一个字段有自己的列表。所以我无法在关卡上执行push()。
如下所示,定义了tradeDetails数组,EXTENSIONS字段有一组值。我如何处理下面这个。
function TradeDetailsToJson(xml) {
var tradeDetails = [];
var tradeId = $(xml).find("tradeHeader tradeId").text();
var tradeDate = $(xml).find("tradeHeader tradeDate").text();
var tradeType = $(xml).find("tradeHeader tradeType").text();
var counterparty = $(xml).find("tradeHeader counterparty").text();
var internalUnit = $(xml).find("tradeHeader internalUnit").text();
var buySell = $(xml).find("tradeHeader buySell").text();
var status = $(xml).find("tradeHeader status").text();
tradeDetails.push({
"tradeId": tradeId,
"tradeDate": tradeDate,
"tradeType": tradeType,
"counterparty": counterparty,
"internalUnit": internalUnit,
"buySell": buySell,
"status": status,
"**extensions**":[]
});
// NOW I'D LIKE TO PUSH MY EXTENSIONS !!
var extName = ""
var extValue = "";
$(xml).find("extensions extension").each(function () {
extName = $(this).find("name").text();
extValue = $(this).find("value").text();
//tradeDetails[extensions].push({ "name": name, "value": value }); ??????
});
}
先谢谢你。 鲍勃
答案 0 :(得分:1)
再次使用推送extensions
属性?
tradeDetails.push({
"tradeId": tradeId,
"tradeDate": tradeDate,
"tradeType": tradeType,
"counterparty": counterparty,
"internalUnit": internalUnit,
"buySell": buySell,
"status": status,
"extensions":[]
});
tradeDetails.extensions.push({ "name": name, "value": value });
根据您的尝试,您需要在引号中包装extensions
这是对象tradeDetails
的关键字。
所以改变
tradeDetails[extensions].push({ "name": name, "value": value });
到
tradeDetails["extensions"].push({ "name": name, "value": value });