JS数组(JSON)整合篇-方法整理
遍历:arr_Param.forEach(function (item, i) {});
反序排序:arr_Param.reverse();
合并数组:arr_Param.push.apply(arr_Param, ["a","b","c"]);
判断数组中是否每个元素都满足条件:arr.every(function(value,index,array){return 条件})
判断数组中是否至少有一个元素满足条件:arr.some(function(value,index,array){return 条件})
/*数组引用赋值*/ var a=[1,2,3]; var b=[]; //数组引用 b=a; //数组赋值 b=[].concat(a);
/*按创建时间排序 字符串时间需转换Date.parse(a.time),IE下横杠替换为/ Date.parse(a.DataTime.replace(/-/g, '/')) */ //倒序 response.sort(function (a, b) { return a.time<b.time?1:-1; }); //升序 response.sort(function (a, b) { return a.time>b.time?1:-1; });
//Json数据排序 Array.prototype.SortJson = function (order, sortBy) { var ordAlpah = (order == 'asc') ? '>' : '<'; var sortFun = new Function('a', 'b', 'return a.' + sortBy + ordAlpah + 'b.' + sortBy + '?1:-1'); return this.sort(sortFun); };
//数组中判断是否存在某元素 Array.prototype.contains = function (needle) { for (i in this) { if (this[i] == needle) return true; } return false; } //数组中删除某指定值元素 Array.prototype.removeByValue = function (val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) { this.splice(i, 1); //break; } } }
//数组中删除某指定值元素(Jquery) var arr = ['a','b','c','d']; arr.splice($.inArray('c',arr),1); alert(arr);
// Array Remove - By John Resig (MIT Licensed) Array.prototype.remove = function(from, to) { var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; return this.push.apply(this, rest); }; /*示例*/ // 移除数组中的第二项 array.remove(1); // 移除数组中的倒数第二项 array.remove(-2); // 移除数组中的第二项和第三项(从第二项开始,删除2个元素) array.remove(1,2); // 移除数组中的最后一项和倒数第二项(数组中的最后两项) array.remove(-2,-1);
JSON数据分组 来源:jb51
[ {"date":"2017-12-22","start_time":"10:00:00","end_time":"10:00:00","status":"Performance Time"}, {"date":"2017-12-22","start_time":"10:40:00","end_time":"10:40:00","status":"Performance Time"}, {"date":"2017-12-23","start_time":"10:00:00","end_time":"10:00:00","status":"Performance Time"}, {"date":"2017-12-23","start_time":"10:40:00","end_time":"10:40:00","status":"Performance Time"} ] //通用 var map = {}, nList = [] //遍历原始数组 for (var i = 0; i < arr.length; i++) { var item = arr[i] //如果map没有则在新nList中添加 if (!map[item.date]) { nList.push({ date: item.date, data: [item] }) map[item.date] = item } else { //遍历nList for (var j = 0; j < nList.length; j++) { var nItem = nList[j]、 //如查找到date符合则添加 if (nItem.date == item.date) { nItem.data.push(item) //跳出循环 break } } } } //使用ES5特性 let map = {}, nList = [] arr.forEach((item) => { if (!map[item.date]) { nList.push({ date: item.date, data: [item] }) map[item.date] = item } else { //因为forEach不支持break,所以使用every实现 nList.every((nItem) => { if (nItem.date === item.date) { nItem.data.push(item) return false } return true }) } }) //性能优化实践 //因数组中的date是按 顺序排列 ,而且没有重复,这样可以考虑 去除第二个循环 let map = {}, nList = [] //设置初始key为0 let _nkey = 0 arr.forEach((item, index) => { if (index === 0) { nList.push({ date: item.date, data: [item] }) } else { let oItem = arr[index - 1] //和前一个date一致则在当前添加,否则添加至nList if (item.date === oItem.date) { nList[_nkey]['data'].push(item) } else { nList.push({ date: item.date, data: [item] }) _nkey ++ } } })