一,数组去重复

function unique(arr) {
// 遍历arr,把元素分别放入tmp数组(不存在才放)
var tmp = new Array();
for (var i in arr) {
//该元素在tmp内部不存在才允许追加
if (tmp.indexOf(arr[i]) == -) {
tmp.push(arr[i]);
}
}
return tmp;
}

使用例子:

    var arr = [];
var narr = [];
for (var i = ; i < data.length; i++) {
Time.push(i);
}
arr.push();
narr = unique(arr);

二,时间格式化,方法

Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + , //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + ) / ), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$, (this.getFullYear() + "").substr( - RegExp.$.length)); //格式化年份
for (var k in o) //循环获取上面定义的月、日、小时等,格式化对应的数据。
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$, (RegExp.$.length == ) ? (o[k]) : (("" + o[k]).substr(("" + o[k]).length)));
return fmt;
}

调用例子:

var time =new Date().Format("yyyy年MM月dd日");
var time2 =new Date().Format("yyyy-MM-dd");
var time3 =new Date("2012-10-11").Format("yyyy年MM月dd日");

三,js的cookie的生成和删除方法

function getCookiex(cookie_name) {
var allcookies = document.cookie;
var cookie_pos = allcookies.indexOf(cookie_name);
if (cookie_pos != -) {
cookie_pos += cookie_name.length + ;
var cookie_end = allcookies.indexOf(";", cookie_pos);
if (cookie_end == -) {
cookie_end = allcookies.length;
}
var value = unescape(allcookies.substring(cookie_pos, cookie_end));
}
return value;
}
function setCookie(c_name, value, expiredays) {
var exdate = new Date()
exdate.setDate(exdate.getDate() + expiredays)
document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}
function delCookie(name) {
var exp = new Date();
exp.setTime(exp.getTime() - );
var cval = getCookiex(name);
if (cval != null)
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

调用例子:

setCookie("search","tt", );
var search = getCookiex("search");
delCookie("search");

四,localStorage的使用

localStorage.setItem('tt',);   //设置
localStorage.getItem('tt'); //获取
localStorage.removeItem('tt'); //删除
localStorage.clear(); //删除所有值。

五,sessionStorage的使用

sessionStorage.setItem('tt',);   //设置
sessionStorage.getItem('tt'); //获取
sessionStorage.removeItem('tt'); //删除
sessionStorage.clear(); //删除所有值。

而,综上,这三种浏览器存储又有什么区别呢?

1,Cookie:存在浏览器端,可设置失效时间,默认是关闭浏览器后失效,大小4K左右,同源窗口中共享,服务器与客户端可互传,Cookie的作用是与服务器进行交互,作为HTTP规范的一部分而存在 ,而Web Storage仅仅是为了在本地“存储”数据而生
2,localStorage:浏览器端,除非被清除,否则永久保存,一般为5MB,同源窗口中共享,仅在客户端(即浏览器)中保存,不参与和服务器的通信
3,sessionStorage:浏览器端,仅在当前会话下有效,关闭页面或浏览器后被清除,一般为5MB,不可在不同浏览器窗口中共享,仅在客户端(即浏览器)中保存,不参与和服务器的通信

Js数据去重复,时间更换格式,cookie,localStorage和sessionStorage的使用等通用方法的更多相关文章

  1. Cookie, LocalStorage 与 SessionStorage

    Cookie, LocalStorage 与 SessionStorage相同点 都是储存在用户本地的数据. 意义在于避免数据在浏览器和服务器间不必要地来回传递. 三者的特点     同属于html5 ...

  2. 区分 Cookie, LocalStorage 与 SessionStorage

    基本概念 Cookie Cookie 的大小限制为4KB左右,是网景公司的前雇员 Lou Montulli 在1993年3月的发明.它的主要用途有保存登录信息,比如你登录某个网站市场可以看到“记住密码 ...

  3. js 数组去重复的方法

    数组去重复是js中常用的方法,归纳了四种如下: 1. for + indexOf  去重复 var arr = [3,5,5,4,1,1,2,3,7,2,5]; var target = []; fo ...

  4. Cookie, LocalStorage 与 SessionStorage说明

     一.Cookie    Cookie 大小限制为4KB左右,不适合大量数据的存储.因为它们由每个对服务器的请求来传递,这使得 cookie 速度很慢而且效率也不高.它的主要用途有保存登录信息,比如你 ...

  5. 详说 Cookie, LocalStorage 与 SessionStorage

    本文最初发布于我的个人博客:咀嚼之味 最近在找暑期实习,其中百度.网易游戏.阿里的面试都问到一些关于HTML5的东西,问题大多是这样开头的:“你用过什么HTML5的技术呀?” 而后,每次都能扯到 Co ...

  6. cookie,localStorage和sessionStorage区别

    三者的异同 特性 Cookie localStorage sessionStorage 数据的生命期 一般由服务器生成,可设置失效时间.如果在浏览器端生成Cookie,默认是关闭浏览器后失效 除非被清 ...

  7. JS 数组去重复值

    var arr1 = [90, 91, 92]; var arr2 = [80, 81]; var arr3 = [80, 71, 72, 73]; var arr = arr1.concat(50, ...

  8. Js数组去重复取唯一值

    function isBigEnough(element) { return element >= 10; } var filtered = [12, 5, 8, 130, 44].filter ...

  9. mysql数据去重复distinct、group by

    使用distinct 和group by都可以实现数据去重. select distinct 字段 group by 一般放在where条件后

随机推荐

  1. 零点.Net Core 接触

    一.Program.cs类与Startup类 1.一切从Main开始,Main方法包含了是整个应用程序的入口 ASP.NET Core应用程序可以配置和启动主机(Host). 主机负责应用程序启动和生 ...

  2. IPC之套接字

    IPC(Inter-Process Communication,进程间通信)实现方式 1)管道: - 管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程之间使用(进程的亲缘关系 ...

  3. 小程序Page里的函数比app.js先执行的解决办法

    问题描述: 当我们初始化一个小程序时,默认文件 app.js 中有onLaunch函数, onLaunch: function () { console.log("onLaunch" ...

  4. Oracle Set操作

    并集合 union/uinon all union 会去重,uinon all 不去重 交集 intersect 差集 minus

  5. CF 82 D.Two out of Three

    前言 全网唯一不同题解 设 \(f[i][j]\) 表示第 \(i\) 次选取留下来的数是 \(k\) 的最小花费 枚举前面的留下来的点 \(k\) 当前能留下的点只有 \((2*i),(2*i+1) ...

  6. python tkinter画圆

    x0=150    #圆心横坐标 y0=100    #圆心纵坐标 canvas.create_oval(x0-10,y0-10,x0+10,y0+10)    #圆外矩形左上角与右下角坐标 canv ...

  7. HDU 5386 Cover

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 题目大意:给一个初始矩阵(n×n).一个目标矩阵(n×n)和m个操作,要求找到一种操作顺序,使初 ...

  8. 【leetcode】1043. Partition Array for Maximum Sum

    题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...

  9. go mac 交叉编译 linux

    CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -o server ./server.go

  10. Feign调用远程服务报错:Caused by: java.lang.IllegalStateException: Method getMemberInfo not annotated with HTTP method type (ex. GET, POST)

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ord ...