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

Taking last word of an string, variable not declared?
Separate string into array and then filter it [duplicate]
Protractor: How to store number text from div element
Weird results with parsing array from json in Swift3
How can I declare an array with diffrent type of data?
Trying to append content to numpy array
MongoDb $addFields inside arrays that $multiply two values within the array
bash: sed search and replace the path of array elements
Scala way for converting Long to ArrayByte
How to Assign Multidimensional Array Values to Tableview Cell swift 3
bring MC to stage with button
Extension file when called in not refreshing (swift3)
SAS - Transform dataset from wide to tall using arrays
How to remove duplicate value in an array Angular 2
How to remove specific object from array? [duplicate]
Index or create array of defined user defined type(VBA)

Categories

HOME
arrays
cntk
sidekiq
pug
redis
tinyos
project-management
oauth
angular-cli
itext
bower
label
google-sheets-api
parse-server
chaiscript
quill
devstack
jpanel
actionscript-2
slim-3
google-plus
uiactivityviewcontroller
squarespace
mod-pagespeed
phpbb
aptana
shippo
uiautomator
google-maps-android-api-2
google-ima
binutils
k2
jquery-waypoints
myob
rhandsontable
web-frontend
system.data.sqlite
websauna
smart-mobile-studio
preg-grep
windows-10-iot-core
geomesa
office365connectors
zero
code-composer
distributed-transactions
finite-element-analysis
resuming-training
mod-fcgid
apiary
3scale
pinvoke
mouseclick-event
tiddlywiki
bnd
rhel6
cgcontext
decompress
crash-reports
forerunnerdb
scd
hpcc
emoticons
lotus
spring-ioc
wcf-ria-services
faraday
project-online
difference
insertion-sort
windows-vista
app.xaml
response-headers
webhdfs
callstack
viewflipper
google-admin-audit-api
korma
cdata
haskell-platform
ftp4j
gridworld
sharpmap
fluidsynth
xml-libxml
sametime
icsharpcode
mozart-mvc
moq-3
serp
goliath
scalaxb
adsl
post-redirect-get
recordset
gears

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile