自我觉得 字符串 数组 cookie localstorage 等等 都是博客必不可少的,我理解的 博客是 自己亲手写的 一个例子一个列子敲过来的,有好的东西要借鉴和分享的,数组的处理这不用说也是必不可少的,以后有好的例子还会增加的。
/*
数组基本功
*/
//(1): 创建数组
var arr1 = new Array(20);
console.log(arr1);
var arr2 = [20];
console.log(arr2);
var arr3 = [‘fdfs‘,‘ewrew‘,‘gfgd‘];
arr3[1] = ‘xxx‘;
arr3[3] = ‘ppp‘;
console.log(arr3);
arr3.length = arr3.length -3;
console.log(arr3);
//(2): join()
var join = [3,1,3,2];
var join1 = join.join(join);
console.log(join1.split(‘=‘));
//(3): push()和pop()
var push = [2,34,‘fd‘,‘sdf‘];
var push1 = push.push(‘22‘);
console.log(push);
var push2 = push.pop();
console.log(push);
//(4): unshift()和shift()
var shift = [343,4324,22];
var shift1 = shift.unshift(‘dd‘);
console.log(shift);
var shift2 = shift.shift();
console.log(shift);
//(5): sort()
var sor = [1,3,2,6,5,3];
var sor1 = sor.sort();
console.log(sor);
function compare(value1,value2){
if(value1 < value2){
return -1;
}else if(value1 > value2){
return 1;
}else{
return 0;
}
};
console.log(sor.sort(compare));
//(6): reverse()
var rever = [32,54,12,43];
var rever1 = rever.reverse();
console.log(rever1);
//(7): concat()
var con = [21,4324,41234,33];
var con1 = con.concat(‘dfs‘,‘fsf‘);
console.log(con1);
//(8) slice()
var sli = [213,12,43,3,1,3];
var sli1 = sli.slice(1,3);
console.log(sli1);
console.log(sli);
//(9): splice()
var sp = [1,2,3,4,5,6];
var sp1 = sp.splice(0,2);//删除
console.log(sp);
var sp2 = sp.splice(1,1,8,9);
console.log(sp);
//(10): indexOf和lastIndexOf()
//(11): forEach()
var each = [2,1,54,2,5];
each.forEach(function(item,index,a){
console.log(item+‘|‘+index+‘|‘+a)
});
//(12): map()
var ma = [2,3,4,1];
var ma1 = ma.map(function(item,index){
return item*item;
});
console.log(ma1);
//(13): filter()
var fil = [1,2,3,4,9,5343];
var fil1 = fil.filter(function(item,index){
return index % 3 === 0 || item >= 8;
});
console.log(fil1);
//(14): every()
var ev = [3,3,1,1,5];
var ev1 = ev.every(function(x){
return x<10;
});
console.log(ev1);
//(15): some()
var so = [1,2,3,4,5];
var so1 = so.some(function(x){
return x<3;
});
console.log(so1);
//(16): reduce()和reduceRight()
var redu = [1,54,22,11];
var redu1 = redu.reduce(function(prev,cur,index,arr){
return prev+cur;
},10);
console.log(redu1)