1

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.

1
  • edited answer, plz check Commented Aug 2, 2017 at 5:53

4 Answers 4

4

push simply adds a value to the end of a (flat) array. Passing multiple parameters to push does not introduce additional levels within the array. Likewise, array indices are purely numeric, and sequential, starting at 0. If you have custom, sparse IDs (which are also strings, in your case), you can't use them as indices to retrieve/store values in your array.

Instead, try something like this:

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);
};

Your results here will look similar to this, which is a typical JSON-like structure:

[
    {
        ChargeTypeId : "1",
        Date : "2017-12-28",
        NoOfRoomsAvailable : "0"
    },
    {
        ChargeTypeId : "23",
        Date : "2017-12-28",
        NoOfRoomsAvailable : "3"
    }
]
Sign up to request clarification or add additional context in comments.

Comments

2

This should work

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 
    };
};

5 Comments

I need the ChargeTypeId to be the key of the array item (either 1, or 23) :)
I'm getting this: [null,{"Date":"2017-12-28","NoOfRoomsAvailable":"0"},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"Date":"2017-12-28","NoOfRoomsAvailable":"3"}]
If you pushed array to a specific index, then in between elements will be undefined/null.
if you want you can take like this structure { "1": { Date : "2017-12-28", NoOfRoomsAvailable : "0" }, "23": { Date : "2017-12-28", NoOfRoomsAvailable : "3" } }
Arrays in Javascript do not work this way. JS does not support associative arrays, you have to work with objects in JS. See nbrooks' or my answer below.
2

The issue you are facing is that JS does not support associative arrays the way that some other languages do. Instead, JS has objects and you will have to live with it. The best thing at this point that you can do is to create:

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

And then you can reference the ID's as obj.ChargeTypeId. This can be achieved as following:

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});
};

Comments

1

you have just need to create new array of ChargeTypeId to allow push. Because currently newArr[ChargeTypeId] not found so its giving you error. Add newArr[ChargeTypeId] = []; before push.

Try this one :

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) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.