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

Summary of common functions for data storage in JavaScript

黄舟
黄舟Original
2017-06-04 10:40:431569browse

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;

 }

Delete elements in the array

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

Save objectsto localStorage

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

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

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!

Statement:
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