jquery从循环中推入多维数组

时间:2017-08-02 05:40:56

标签: jquery for-loop multidimensional-array

我正在尝试通过执行以下操作在for循环中创建一个数组:

<input type="checkbox" value="" ng-model="t.IsPullPoint" onclick="return false;" onkeydown="return false;"><span class="cr"></span></label>

这是创造:

var newArr = [];
for(var j=0; j < ChargeTypes.ChargeType.length; j++){
    var ChargeTypeId = ChargeTypes.ChargeType[j].ChargeTypeId;
    var Date = ChargeTypes.ChargeType[j].Charge[0]['@attributes'].Date;
    var NoOfRoomsAvailable = ChargeTypes.ChargeType[j].Charge[0]['@attributes'].NoOfRoomsAvailable;
    newArr.push({ChargeTypeId},{Date});
    newArr.push({ChargeTypeId},{NoOfRoomsAvailable});
};
alert( JSON.stringify(newArr) );

但我希望能得到这样的东西:

[
    {"ChargeTypeId":"1"},
    {"Date":"2017-12-28"},
    {"ChargeTypeId":"1"},
    {"NoOfRoomsAvailable":"0"},
    {"ChargeTypeId":"23"},
    {"Date":"2017-12-28"},
    {"ChargeTypeId":"23"},
    {"NoOfRoomsAvailable":"3"}
]

我尝试过使用它:

[1] //ChargeTypeID
    {
        Date:"2017-12-28",
        NoOfRoomsAvailable:"0"
    },
[23] //ChargeTypeID
    {
        Date:"2017-12-28",
        NoOfRoomsAvailable:"3"
    }

但是我收到了这个错误:

TypeError:newArr [ChargeTypeId]未定义

我显然错过了一些东西,这可能是小事,但如果有人能帮助我,那就太棒了。

4 个答案:

答案 0 :(得分:2)

这应该有效

var newArr = [];
for(var j=0; j < ChargeTypes.ChargeType.length; j++){
    var ChargeTypeId = ChargeTypes.ChargeType[j].ChargeTypeId;
    var Date = ChargeTypes.ChargeType[j].Charge[0]['@attributes'].Date;
    var NoOfRoomsAvailable = ChargeTypes.ChargeType[j].Charge[0]['@attributes'].NoOfRoomsAvailable;
    newArr[parseInt(ChargeTypeId)] = {
        "Date": Date,
        "NoOfRoomsAvailable": NoOfRoomsAvailable 
    };
};

答案 1 :(得分:2)

push只是在(平面)数组的末尾添加一个值。将多个参数传递给push不会在数组中引入其他级别。同样,数组索引是纯数字的,也是顺序的,从0开始。如果您有自定义的稀疏ID(在您的情况下也是字符串),则不能将它们用作索引来检索/存储数组中的值。

相反,尝试这样的事情:

var newArr = [];

for(var j = 0; j < ChargeTypes.ChargeType.length; j++){
    var chargeType = ChargeTypes.ChargeType[j];
    var chargeAttribute = chargeType.Charge[0]['@attributes'];
    var chargeDetails = {
        ChargeTypeId : chargeType.ChargeTypeId,
        Date : chargeAttribute.Date,
        NoOfRoomsAvailable : chargeAttribute.NoOfRoomsAvailable
    };

    newArr.push(chargeDetails);
};

此处的结果与此类似,这是一种典型的类JSON结构:

[
    {
        ChargeTypeId : "1",
        Date : "2017-12-28",
        NoOfRoomsAvailable : "0"
    },
    {
        ChargeTypeId : "23",
        Date : "2017-12-28",
        NoOfRoomsAvailable : "3"
    }
]

答案 2 :(得分:2)

您面临的问题是JS不像其他语言那样支持关联数组。相反,JS有对象,你将不得不忍受它。此时您可以做的最好的事情是创建:

$obj = [
    {"ChargeTypeId":"1", "Date":"2017-12-28", "NoOfRoomsAvailable":"0"},
    {"ChargeTypeId":"23", "Date":"2017-12-28", "NoOfRoomsAvailable":"3"}
]

然后您可以将ID引用为obj.ChargeTypeId。这可以通过以下方式实现:

var newArr = [];
for(var j=0; j < ChargeTypes.ChargeType.length; j++){
    var Date = ChargeTypes.ChargeType[j].Charge[0]['@attributes'].Date;
    var NoOfRoomsAvailable = ChargeTypes.ChargeType[j].Charge[0]['@attributes'].NoOfRoomsAvailable;
    newArr.push({ChargeTypeId,Date,NoOfRoomsAvailable});
};

答案 3 :(得分:1)

您只需要创建new array ChargeTypeId来允许推送。因为目前newArr[ChargeTypeId]找不到所以它给你错误。在推送之前添加newArr[ChargeTypeId] = [];

试试这个:

var newArr = [];
for(var j=0; j < ChargeTypes.ChargeType.length; j++){
    var ChargeTypeId = ChargeTypes.ChargeType[j].ChargeTypeId;
    var Date = ChargeTypes.ChargeType[j].Charge[0]['@attributes'].Date;
    var NoOfRoomsAvailable = ChargeTypes.ChargeType[j].Charge[0]['@attributes'].NoOfRoomsAvailable;
    newArr[ChargeTypeId] = [];
    newArr.push({ChargeTypeId},{Date});
    newArr.push({ChargeTypeId},{NoOfRoomsAvailable});
};
alert( JSON.stringify(newArr) );