11、计算出数组中出现次数最多的元素
var arrayObj = [1,1, 2, 3, 3, 3,4, 5, 5]; var tepm = '',count =0; var newarr = new Array(); for(var i=0;i<arrayObj.length;i++){ if (arrayObj[i] != -1) { temp = arrayObj[i]; } for(var j=0;j<arrayObj.length;j++){ if (temp == arrayObj[j]) { count++; arrayObj[j] = -1; } } newarr.push(temp + ":" + count); count = 0; } for(var i=0;i<newarr.length;i++){ console.log(newarr[i]); }12、数组filter(搜索功能)
var fruits = ['apple','banana', 'grapes','mango', 'orange']; function filterItems(query) { return fruits.filter(function(el) { return el.toLowerCase().indexOf(query.toLowerCase()) > -1; }) } console.log(filterItems('ap')); // ['apple', 'grapes']13、copy 对象(第一种)
//第一种 var cloneObj =function(obj) { var newObj = {}; if (obj instanceof Array) { newObj = []; } for (var keyin obj) { var val = obj[key]; newObj[key] = typeof val === 'object' ? cloneObj(val) : val; } return newObj; }; //第二种 function clone(origin , target){ var target = target || {}; for(var propin origin){ target[prop] = origin[prop]; } return target; }14、深度克隆
var newObj ={}; function deepClone(origin,target){ var target = target || {}, toStr = Object.prototype.toString, arrStr = "[object Array]"; for(var propin origin){ if(origin.hasOwnProperty(prop)){ if(origin[prop] != "null" && typeof(origin[prop]) == 'object'){//判断原型链 target[prop] = (toStr.call(origin[prop]) == arrStr) ? [] : {}//判断obj的key是否是数组 deepClone(origin[prop],target[prop]);//递归的方式 }else{ target[prop] = origin[prop]; } } } return target } deepClone(obj,newObj); console.log(newObj)15、求数组最大值和最小值
Array.prototype.max = function(){ return Math.max.apply({},this) } Array.prototype.min = function(){ return Math.min.apply({},this) } console.log([1,5,2].max())16、json数组去重
function UniquePay(paylist){ var payArr = [paylist[0]]; for(var i =1; i < paylist.length; i++){ var payItem = paylist[i]; var repeat = false; for (var j =0; j < payArr.length; j++) { if (payItem.name == payArr[j].name) { repeat = true; break; } } if (!repeat) { payArr.push(payItem); } } return payArr; }17、对比两个数组,取出交集
Array.intersect = function () { var result = new Array(); var obj = {}; for (var i =0; i < arguments.length; i++) { for (var j =0; j < arguments[i].length; j++) { var str = arguments[i][j]; if (!obj[str]) { obj[str] = 1; } else { obj[str]++; if (obj[str] == arguments.length) { result.push(str); } }//end else }//end for j }//end for i return result; } console.log(Array.intersect(["1","2", "3"], ["2","3", "4", "5", "6"]))18、数组和对象比较。取出对象的key和数组元素相同的
var arr = ['F00006','F00007','F00008']; var obj = {'F00006':[{'id':21}],'F00007':[{'id':11}]} var newobj = {}; for(var itemin obj){ if(arr.includes(item)){ newobj[item] = obj[item] } } console.log(newObj)19、删除数组中某个元素
//第一种 Array.prototype.remove = function(val){ var index = this.indexOf(val); if(index !=0){ this.splice(index,1) } } [1,3,4].remove(3) //第二种 function remove(arr, indx) { for (var i =0; i < arr.length; i++) { var index = arr.indexOf(arr[i]); if (indx == index) { arr.splice(index, 1) } } return arr }20、判断数组是否包含某个元素
Array.prototype.contains = function (val) { for (var i =0; i < this.length; i++) { if (this[i] == val) { return true; } } return false; }; [1, 2,3, 4].contains(2)//true
哈尔滨品用软件有限公司致力于为哈尔滨的中小企业制作大气、美观的优秀网站,并且能够搭建符合百度排名规范的网站基底,使您的网站无需额外费用,即可稳步提升排名至首页。欢迎体验最佳的哈尔滨网站建设。