1.如何判断数组元素是否存在重复项 1)定义测试数组 //定义测试的数组(1个没有重复元素,1个有重复元素) var arr1 = new Array("111","333","222","444"); var arr2 = new Array("aa","cc","bb","dd","bb"); 2) 判断数组元素重复的方法
function isRepeat(arr) { var hash = {}; for(var i in arr) { if(hash[arr[i]]) return true; hash[arr[i]] = true; } return false; } var a = [1,2,34,5,1] console.log(isRepeat(a))
项目背景需求是: 已知: var a=[{name:'jenny',age:18},{name:'john',age:19},{name:'jack',age:20}] var b ='jenny' 返回: {name:'jenny',age:18} 解题思路:现将数组转成字符串,判断改数组中是否存在该字符串,存在的情况下遍历该数组,返回匹配的数据 代码如下:
1.代码如下: var toObject = function(a) { var o = {}; for (var i=0, j=a.length; i<j; i=i+1) { // 这里我调整了下, YUI源码中是i<a.length o[a[i]] = true; } return o; } var keys = function(o) { var a=[], i; for (i in o) { if (o.hasOwnProperty(i)) { // 这里, YUI源码中是lang.h
$("form").submit(function () { var content = editor.getContentTxt(); var sum = 0; re = /[\u4E00-\u9FA5]/g; //测试中文字符的正则 if (content) { if (re.test(content)) //使用正则判断是否存在中文 { if (content.match(re).length <= 10) { //返回中文的个数 $.dialog.tips("帖
[抄题]: Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exce