Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to JavaScript, Having a problem with an array

Status
Not open for further replies.

Disskyz

Programmer
Aug 29, 2018
7
US
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.Package.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?





 
Thank mikrom, I was able to figure it out!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top