版权声明:本文为吴孔云博客原创文章,转载请注明出处并带上链接,谢谢。 https://cloud.tencent.com/developer/article/1347602
function isRepeat(arrs) {
if(arrs.length > 0) {
var sArrs = arrs.sort();
for(var i = 0,ilen = sArrs .length; i < ilen; i+=1) {
if(sArrs [i] == sArrs [i+1]) {
return true;
}
}
}
return false;
}
var arrs = [1,2,3];
console.log(isRepeat(arrs)); // false
arrs.push(2)//4
console.log(isRepeat(arrs)); // true
/*
* 逻辑思路:定义一个对象 遍历数组,判断该对象的键有没有,有的话返回true,否则返回false
*/
function isRepeat(arrs) {
var has = {};
if(arrs.length > 0) {
for(var i = 0, len = arrs.length; i < len ; i=i+1) {
if(has[arrs[i]]){//利用数组元素值作为对象下标
return true;
}
has[arrs[i]] = true;
}
}
return false;
}
var arrs = ['a','b','c','d'];
isRepeat(arrs)//false
arrs.push('a')//5
isRepeat(arrs)//true
function arrayElemCount(arrs) {
var newArrs = [];
if(arrs.length > 0) {
for(var i = 0,ilen = arrs.length; i < ilen; i+=1) {
var temp = arrs[i];
var count = 0;
for(var j = 0,jlen = arrs.length; j < jlen; j+=1) {
if(arrs[j] == temp) {
count++;
arrs[j] = -1;//判断再赋值
}
}
newArrs.push(temp + ":" +count);
}
}
return newArrs;
}
var arrs = [1,2,1,2,3,4,5,6,7,1,2,3];
console.log(arrayElemCount(arrs));
// 打印如下:["1:3", "2:3", "-1:6", "-1:6", "3:2", "4:1", "5:1", "6:1", "7:1", "-1:12", "-1:12", "-1:12"]
// 键值为-1的,都可以去掉
function arrayElemCount(arrs) {
if(arrs.length > 0){
for(var i = 0, len = arrs.length; i < len ; i=i+1) {
var count = 0;
var a = arrs[i];
for(var j = 0;j < len;j++){
if(arrs[j] == a ){
count++;
}
}
var as = true;
for(var k = 0;k < i;k++ ){
if(arrs[k]==arrs[i]){
as = false;
}
}
if(as){
console.log(arrs[i]+':'+count)
}
}
}
}
var arrs = [1,2,1,2,3,4,5,6,7,1,2,3];
arrayElemCount(arrs);//做一个条件赋值判断,为false不打印出来
/* 重复项 indexOf方法在IE8及IE8以下不支持,因此有必要封装一个
* @return {newArrays} 返回新数组
*/
function arrayUnique(arrs) {
var newArrays = [];
for(var i = 0,ilen = arrs.length; i < ilen; i++) {
if(newArrays.indexOf) {
if(newArrays.indexOf(arrs[i]) == -1) {
newArrays.push(arrs[i]);
}
}else {
if(indexOf(arrs[i],newArrays) == -1) {
newArrays.push(arrs[i]);
}
}
}
return newArrays;
}
/*
* 为了支持IE8及以下浏览器需要封装一个indexOf方法
* @param {arr,arrs} 某一项 原数组
* @return 返回数组某一项
*/
function indexOf(arr,arrs){
if(arrs.length > 0) {
for(var i = 0,ilen = arrs.length; i < ilen; i+=1) {
if(arrs[i] == arr) {
return i;
}
}
}
return -1;
}
var arrs = [1,2,3,1,2,3];
console.log(arrayUnique(arrs)); // [1,2,3]
//数组下标判断法
function arrayUnique(arrs) {
var newArrays = [];
if(arrs.length > 0) {
for(var i = 0,ilen = arrs.length; i < ilen; i+=1) {
if(arrs.indexOf) {
if(arrs.indexOf(arrs[i]) == i) {
newArrays.push(arrs[i]);
}
}else {
if(indexOf(arrs[i],arrs) == i) {
newArrays.push(arrs[i]);
}
}
}
}
return newArrays;
}
//数组排序对比法、循环对比新数组
function arrayUnique(arrs) {
var newArrays = [];
if(arrs.length > 0) {
arrs.sort();
for(var i = 0,ilen = arrs.length; i < ilen; i+=1) {
//此处是跟newArrays的数组对比
if(arrs[i] !== newArrays[newArrays.length - 1]) {
newArrays.push(arrs[i]);
}
}
}
return newArrays;
}
//对象键值对法
function arrayUnique(arrs) {
var newArrays = [];
var hash = {};
if(arrs.length > 0) {
for(var i = 0,ilen = arrs.length; i < ilen; i+=1) {
if(!hash[arrs[i]]) {
hash[arrs[i]] = 1;
newArrays.push(arrs[i]);
}
}
}
console.log('hash'+hash);
return newArrays;
}
function getArrsSameAndDiffElem(arrs1,arrs2){
var hash = {},
sameElemArrs = [],
diffElemArrs = [];
if(arrs1.length > 0) {
for(var i = 0,ilen = arrs1.length; i < ilen;i+=1) {
hash[arrs1[i]] = 1;
}
}
if(arrs2.length > 0) {
for(var j = 0,jlen = arrs2.length; j < jlen; j+=1) {
if(hash[arrs2[j]]) {
// 说明有相同的元素,把相同的元素存入sameElemArrs数组里面去
sameElemArrs.push(arrs2[j]);
}else {
// 说明是不同的元素,把不同的元素存入diffElemArrs数组里面去
diffElemArrs.push(arrs2[j]);
}
}
}
return {
sameElemArrs: sameElemArrs,
diffElemArrs: diffElemArrs
}
}
var arrs1 = ["aac","aab","cfg",'longen','tugenhua','single'];
var arrs2 = ["aac","mnc","nba","cba","anta",'tugenhua','single'];
console.log(getArrsSameAndDiffElem(arrs1,arrs2));
Array.prototype.slice.call(arguments); 即可将类数组转换为数组||[].slice.call(arguments)
var arr = [["wu", "xia"], ["kong", "tao"], ["yun", "ou"]];
//不确定长度数组,使用递归
function doExchange(doublearrays) {
var len = doublearrays.length;
if(len >= 2) {
var arr1 = doublearrays[0],
arr2 = doublearrays[1];
var len1 = arr1.length,
len2 = arr2.length;
var index = 0;
var totalLen = len1 * len2;
var newArray = new Array(totalLen);
for(var i=0; i<len1; i++) {
for(var j=0; j<len2; j++) {
newArray[index] = arr1[i] + arr2[j];
index ++ ;
}
}
var tempArr = [];
tempArr[0] = newArray;
for(var k=2; k<len; k++) {
tempArr[k-1] = doublearrays[k];
}
return doExchange(tempArr);
}else {
return doublearrays[0];
}
}