I'm trying to create an array within a for loop, by doing the following:
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) );
This is creating:
[
{"ChargeTypeId":"1"},
{"Date":"2017-12-28"},
{"ChargeTypeId":"1"},
{"NoOfRoomsAvailable":"0"},
{"ChargeTypeId":"23"},
{"Date":"2017-12-28"},
{"ChargeTypeId":"23"},
{"NoOfRoomsAvailable":"3"}
]
But what I'm hoping for is to get something like this:
[1] //ChargeTypeID
{
Date:"2017-12-28",
NoOfRoomsAvailable:"0"
},
[23] //ChargeTypeID
{
Date:"2017-12-28",
NoOfRoomsAvailable:"3"
}
I've tried using this:
newArr[ChargeTypeId].push("Date",Date);
newArr[ChargeTypeId].push("NoOfRoomsAvailable",NoOfRoomsAvailable);
But I get this error:
TypeError: newArr[ChargeTypeId] is undefined
I'm clearly missing something, and it's probably something small lol but if anyone could help me out, that would be fantastic.