Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

New to JavaScript, Having a problem with an array

New to JavaScript, Having a problem with an array

New to JavaScript, Having a problem with an array

I have an object that I manually defined and populated. Looks like this: {"Shipment": [{"Package":[{"TrackingNumber":"", "DeliveryDate":[{"Type":"", "Date:""}], "DeiveryTime":"StartTime:"", "EndTime":"", "Type":""}, "Activity":["Location":{"Address":{"City":"", "StateProvince":"", "PostalCode":"", "CountryCode":""}}, "Status:{"Type":"", "Description":"". "Code":""}, "Date":"", "Time":""}]}]}]};
As you can see, DeliveryDate and Activity are in array. In my code, I am reading a JSON object and writing a different JSON object that looks like the object above. However, the problem is I am only writing one occurence of the DeliveryDate and Activity array. It appears that I am overwriting the object. I am trying to use the push function to write the objects.
first, I defined the exterior object like this:
var trackResponse = {}; trackResponse.Shipment =[]; var pkgObj = {}; trackResponse.Shipment.push(pkgObj); pkgObj.Package[]; This allows me to write the base array, as there can only be one Shipment and one Package even though it is an array.
Then I tried to push the Tracking number using: trackResponse.Shipment[i].Package[i].push({"TrackingNumber": value}); this line of code works for me. I am able to push the next array object as var delObj={}; pkgObj.Package.push(delObj); pkgObj.Package.DeliveryDate =[]; I can push the values to the DeliveryDateObject using pkgObj.Package.DeliveryDate.push({"Type":value}) and pkgObj.Package.DeliveryDate.push({"Date":value});
The problem comes in when I try to push the DeliveryTime object. I defined it as var dTimeObj = {}; pkgObj.Package.push(dTimeObj); pkgObj.Package.DeliveryTime={}; So when I try to push the StartTime to the object, the code fails. This is what I have: pkgObj.Package.DeliveryTime.push({"StartTime": value});
Not sure why it fails, but could it be that the push function only works on an array and not an object? If so, how would I add just the single object?

RE: New to JavaScript, Having a problem with an array

the JSON you posted is not valid

RE: New to JavaScript, Having a problem with an array

Thank mikrom, I was able to figure it out!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close