Home > Web Front-end > JS Tutorial > Summary of common functions for data storage in JavaScript

Summary of common functions for data storage in JavaScript

黄舟
Release: 2017-06-04 10:40:43
Original
1609 people have browsed it

This article mainly introduces javascript data storage common functions summary related information, friends in need can refer to

common functions of data storage

Save arraynon-duplicate values

function pushtoArray(myarr,mydata){

   if(myarr.length==0){

     myarr.push(mydata);
   }else{
     var oktopush=true;
     for(var ele in myarr){


       if(myarr[ele]==mydata){
         oktopush=false;



       }
     }

     if(oktopush){
       myarr.push(mydata);
     }


   }

   return myarr;

 }
Copy after login

Delete elements in the array

Array.prototype.remove = function(val) {
     var index = this.indexOf(val);
     if (index > -1) {
       this.splice(index, 1);
     }
   };
Copy after login

Save objectsto localStorage

function setObjectStorage(itemname,myobj){
   localStorage.setItem(itemname, JSON.stringify(myobj));
 }

 function getObjectStorage(itemname){
   return JSON.parse(localStorage.getItem(itemname));
 }
Copy after login

The above is the detailed content of Summary of common functions for data storage in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template