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

Python loop to print salaries within range of the average
How to display this data in twig
How to properly parse a dictionary array
Sort array [[String: Any]] alphabetically in swift 3.0 [duplicate]
In Ruby, how do I execute “select_with_index” on an array?
Is it possible to turn an array to a Result<> in Realm?
MongoDB resolve each ObjectId in an array field to an object from another collection
Play framework, parse json array from request
Swift array of Generics with protocols [duplicate]
How to reverse a given array of numbers in Python 3?
Saving an array of Integers using UserDefaults
delphi Send a file as byte array to a Rest service
Most frequent element in an array (bash 3.2)
User defined function changing each value in an array
swift vapor json response to array
Mongodb inserting into nested array of objects

Categories

HOME
sharepoint
paypal
google-apps-script
dotnetrdf
caching
cntk
erlang
steam
aem
avro
gspread
constructor
cakephp-2.9
datagrip
mod-pagespeed
sendkeys
lstm
jquery-terminal
android-toolbar
ng-show
ms-access-2007
styles
multicore
flexboxgrid
statusbar
countif
kitematic
spring-ws
qsub
croppic
data-cleansing
codelite
mef2
devforce
crt
preg-grep
inkscape
brute-force
stereo-3d
onmouseover
jtds
spring-data-hadoop
component-pascal
qos
payu
cfeclipse
c#-interactive
chessboard.js
inotifypropertychanged
gmsmapview
mongo-c-driver
interactive-brokers
galaxy
p-np
connection-reset
ytplayerview
ffprobe
viper-architecture
epl
jython-2.7
sysfs
orientation-changes
qtestlib
mser
mov
io.js
funkload
data-generation
flash-cc
pyjade
html-form-post
windows-update
shortcuts
transitive-closure-table
code-conversion
gdt
parameterization
nintendo
nssavepanel
hashalgorithm
ajax.beginform
drawtext
winverifytrust
code-design
castle-validators
rd

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