Storing arrays and objects using Sessionstorage, Localstorage

Source: Internet
Author: User
Tags sessionstorage

http://my.oschina.net/crazymus/blog/371757 using Sessionstorage, Localstorage storage arrays and objects posted 3 month ago (2015-01-26 12:11) Read (708) | Comments (0) 5 People collection This article, I want to collect likes 0

Mu-NET, programmer promotion raise pay artifact, click Free to learn

Mobile Web

Sometimes we need to store the data in Sessionstorage and Localstorage, and the benefits are:

1 Caching data

2 Reduction in memory footprint

However, storage can only store strings of data, for the usual array or object in JS can not be stored directly.

?
1234567 varobj = { name:‘Jim‘ };sessionStorage.obj = obj; localStorage.obj = obj; vararr = [1,2,3]; sessionStorage.obj = arr; localStorage.obj = arr;

The above is not a successful formulation! But we can convert other data types into strings through the parse and stringify provided by the JSON object, and then store them in storage. Take a look at the following code.

?
123456789 varobj = { name:‘Jim‘ }; var str = JSON.stringify(obj); //存入 sessionStorage.obj = str; //读取 str = sessionStorage.obj; //重新转换为对象 obj = JSON.parse(str);


Localstorage is the same, but the storage time is not the same as sessionstorage.

It is important to note that the array in JS is essentially an object type, so the above code is also applicable to arrays.

Storing arrays and objects using Sessionstorage, Localstorage (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.