0

我可以使用 push({}) 创建我的 javascript 数组,但是我的一个字段有自己的列表。所以我在水平下降时遇到了麻烦。

如下,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 }); ??????
});
}

谢谢你。鲍勃

4

1 回答 1

1

再次使用 pushextensions属性?

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哪个是 object 的键。tradeDetails

所以改变

tradeDetails[extensions].push({ "name": name, "value": value });

tradeDetails["extensions"].push({ "name": name, "value": value });
于 2013-06-06T22:21:31.743 回答