arrays


localstorage initially returns assigned type, then type Object


I am developing a shopping cart which enables users to add items of type "Product" to basket. When an item is initially added it is of type "Product", however when another item(second item) is added to the Array() array, it is of type "Object" from then on. This may not make a lot of difference during manipulation, however i'd rather keep the types consistent. Someone please point out where im going wrong, much appreciated.
constructor(private _http: Http) {
localStorage.setItem("cart", JSON.stringify(new Array<Product>()));
}
GetCartProducts() {
let storage:string = localStorage.getItem("cart");
return <Array<Product>>((storage) ? JSON.parse(storage) : new Array<Product>());
}
AddProduct(product: Product) {
let items = <Array<Product>>this.GetCartProducts();
console.log(items);
items.push(<Product>product);
console.log(items);
localStorage.setItem("cart", JSON.stringify(<Array<Product>>items));
}

Related Links

If-else inside a foreach doesn't return what I think it should
Helper function to iterate over an array in a rotating fashion
R paired t test between each row of 3d array
How to better use repeat while loop for indexing and array
MongoDB array duplicates
How to store input from a loop to an array in assembly(mips). How to store the integers I read in arrA?
C assembly error for a given array
Excel Array formula using INDEX and is variable dependant
Filter data from array Angular 2
Writing to an HDF5 file in parallel using chunking in fortran
remove oldest N elements from MongoDB array
How to display array elements in UITextField in swift 3?
Reshape NxNxM array to MxNxN
Adding count to a vector while looping through dataframe
Using arrays to create a set of variables representing the non-blank values of a separate set of variables
How to get all the elements from array of tuples matching String?

Categories

HOME
sql-server-2008
drupal-7
performancecounter
ncurses
computer-vision
azureservicebus
pca
flexbox
camera-calibration
uicollectionview
styles
jive
tweets
excel-2010
delete-file
csh
shopping-cart
emv
dss
pentaho-report-designer
xilinx-ise
google-api-dotnet-client
bitcoin-testnet
dd-wrt
sharp
pep8-assembly
canvasjs
pljson
web-audio-api
reactivemongo
superpowered
openshift-enterprise
s3cmd
php-ews
floor
mouseclick-event
crystal-reports-8.5
castle-dynamicproxy
createobject
svn-merge
packagemaker
children
dbscan
uwp-maps
string-parsing
unity5.3
integral
code-first-migrations
searchkick
jai
cpan
cpu-speed
adcolony
sthttprequest
system.web
smartystreets
asp.net-mvc-scaffolding
nscolor
haskell-platform
lame
shiva3d
pep8
htmltextwriter
n-layer
symfony-2.0
xmlslurper
gdt
hobo
dcpu-16
nagle
.app
pureftpd
sitemappath
camtasia
file-encodings
trampolines
webrat

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App