[toc]数组
let x = 5;
let y = 7;
[y,x] = [x,y];
console.log(x,y);
复制代码
String.prototype.repeatify = function(n){
return (new Array(n+1).join(this));
}
console.log('Hello'.repeatify(3)); // HelloHelloHello
复制代码
String.prototype.computer = function(str){
let index = []
let wordLen = str.length
for(let i = 0; i < (this.length - wordLen); i++){
let sonStr = this.slice(i,i+wordLen);
if(sonStr == str){
index.push(i)
}
}
console.log(index)
console.log('"'+str+'"共出现了'+index.length+'次')
}
let str = '这是一段寻找屡次出现的词汇的文本,根据输入的词汇,搜索该词汇出现次数,并使该词汇高亮';
str.computer('词汇'); // [11, 22, 28, 38] "词汇"共出现了4次
str.computer('出现'); // [8, 30] "出现"共出现了2次
str.computer('该词汇'); // [27, 37] "该词汇"共出现了2次
复制代码
String.prototype.appearMax = function(obj){
let name = ''
let repeat = 0
for(let o in obj){
if(obj[o] > obj['max']){
obj['max'] = obj[o]
name = o //
repeat = obj[o]
}
}
return '出现次数最多的字符是' + name + ',共出现' + repeat + '次'
}
String.prototype.everyNumber = function(){
let obj = {
max: 0
}
let strArr = this.split("")
for(let i = 0; i < strArr.length; i++){
if(obj.hasOwnProperty(strArr[i])){
obj[strArr[i]]++
}else{
obj[strArr[i]] = 1
}
}
console.log(obj)
return this.appearMax(obj)
}
console.log('aabbfffffaaffddff'.everyNumber()) // { a: 4, b: 2, f: 9, d: 2 } 出现次数最多的字符是f,共9次
复制代码
String.prototype.tuoFeng = function(){
let arr = this.split('-');
let sFo = arr[0];
for(let i =1; i < arr.length; i++){
let i_0 = arr[i].charAt(0).toLocaleUpperCase();
let l = arr[i].length;
let i_1 = arr[i].slice(1,l);
sFo += (i_0 + i_1);
}
console.log(sFo);
}
'get-user-by-name'.tuoFeng()
复制代码
此为5.1和5.2共用类:数据结构
class latinPin{
constructor() {
this.yuan = ['a','e','i','o','u']
}
test(str){ // 拉丁猪文字游戏
if(this.yuan.indexOf(str[0]) < 0){ // 若是第一个就是辅音的话
let first = str[0];
let last = str.slice(1,str.length)
return last+'-'+first+'ay'
}
let arr = str.split('');
for(let i = 1; i< arr.length; i++){ // 以元音字母开头的话
if(this.yuan.indexOf(str[i]) < 0){
let toEnd = str[i];
let frist = str.slice(0,i);
let last = str.slice(i+1,str.length)
return frist+last+'-'+toEnd+'ay'
}
}
}
computer(str){ // 统计字符串中每一个元音字母的个数
let arr = str.split('');
let obj ={}
for(let i = 0; i < arr.length; i++){
if(this.yuan.indexOf(arr[i]) >= 0){
if(obj.hasOwnProperty(arr[i])){
obj[arr[i]]++
}else{
obj[arr[i]] = 1
}
}
}
console.table(obj)
}
}
复制代码
let ltp = new latinPin();
ltp.test('banana') // anana-bay
ltp.test('orange') // oange-ray
ltp.test('apple') // aple-pay
ltp.test('watermelon') // atermelon-way
复制代码
ltp.computer('banana') // { a: 3 }
ltp.computer('orange') // { o: 1,a: 1,e: 1}
ltp.computer('apple') // { a: 1,e: 1 }
ltp.computer('watermelon') // {a: 1,e: 2,o: 1}
复制代码
let arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
复制代码
现需将其每隔5位截取为一组,每一组的前两位和后两位对调位置:app
Array.prototype.edcba = function(){
let n = -5,m = 0
let loop = this.length / 5
let result = []
for(let i = 0; i<loop; i++){
let son = this.slice(n+=5,m+=5).reverse()
result = result.concat(son)
}
return result
}
console.log(arr.edcba()) // [5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 15, 14, 13, 12, 11]
复制代码
Array.prototype.decab = function(abc){
let a = abc.slice(0,2)
let b = abc[2]
let c = abc.slice(3,5)
return c.concat(b,a)
}
Array.prototype.arrSplit = function(){
let n = -5,m = 0
let loop = this.length / 5
let result = []
for(let i = 0; i < loop; i++){
let son = this.slice(n+=5,m+=5)
result = result.concat(this.decab(son))
}
return result
}
console.log(arr.arrSplit()) // [4, 5, 3, 1, 2, 9, 10, 8, 6, 7, 14, 15, 13, 11, 12]
复制代码
[点击这里看-JS递归实现DOM树状结构](https://juejin.im/post/5da67313f265da5bab5bd257).
< !-- 待更新 --> 复制代码