Javascript 字符串和数组的常用函数总结
1. 字符串函数2 数组函数
1. 字符串函数
testCharAt() {
const str
= "Hello World!";
let a
= str
.charAt(0);
let b
= str
.charAt(30);
console
.log(a
);
console
.log(b
);
},
testConcat() {
let a
= "Hello ";
let b
= "World";
let c
= a
.concat(b
);
console
.log(a
);
console
.log(b
);
console
.log(c
);
},
testIndexOf() {
let a
= "Hello World";
console
.log(a
.indexOf("ll"));
console
.log(a
.indexOf(" "));
console
.log(a
.indexOf("Wor"));
console
.log(a
.indexOf("wor"));
console
.log(a
.indexOf("www"));
},
testIncludes() {
let a
= "Hello World";
console
.log(a
.includes("Wor"));
console
.log(a
.includes("wor"));
},
testLastIndexOf() {
let a
= "Hello World";
console
.log(a
.lastIndexOf("Wor"));
console
.log(a
.lastIndexOf("wor"));
},
testReplace() {
let a
= "Hello World";
console
.log(a
.replace("World", "Pigg"));
},
testSlice() {
let a
= "Hello World";
let b
= a
.slice(1, 3);
console
.log(a
);
console
.log(b
);
},
testSplit() {
let a
= "Hello World";
let b
= a
.split(" ");
console
.log(a
);
console
.log(b
);
},
testStartsWith() {
let a
= "Hello World";
console
.log(a
.startsWith("Hel"));
console
.log(a
.startsWith("hel"));
},
testSubstr() {
let a
= "Hello World";
let b
= a
.substr(1, 3);
let c
= a
.substr(1);
console
.log(a
);
console
.log(b
);
console
.log(c
);
},
testSubstring() {
let a
= "Hello World";
let b
= a
.substring(1, 3);
let c
= a
.substring(1);
console
.log(a
);
console
.log(b
);
console
.log(c
);
}
2 数组函数
let vm
= new Vue({
el
: "#app1",
data
: {
arr
: [1, 2, 3, 4],
message
: "hello vue.js!!!"
},
methods
: {
checkMoreThanTwo(num
) {
return num
> 2;
},
testContact() {
let a
= ["a", "aa"];
let b
= ["b", "bb"];
let c
= a
.concat(b
);
console
.log(a
);
console
.log(b
);
console
.log(c
);
},
testIncludes() {
let a
= ["a", "aa"];
console
.log(a
.includes("a"));
console
.log(a
.includes("aaa"));
},
testMap() {
let newMapArr
= this.arr
.map(function (item
) {
return item
* 2;
})
this.message
= newMapArr
;
},
testPop() {
let a
= ["a", "aa"];
let b
= a
.pop();
console
.log(a
);
console
.log(b
);
},
testPush() {
let a
= ["a", "aa"];
let b
= a
.push("aaa", "aaaa");
console
.log(a
);
console
.log(b
);
},
testFind() {
let arr2
= this.arr
.find((n
) => n
> 1);
this.message
= arr2
;
},
testFindIndex() {
let index
= this.arr
.findIndex((n
) => n
> 0);
this.message
= index
;
},
testFilter() {
let filterArr
= this.arr
.filter((n
) => n
> 3);
this.message
= filterArr
;
},
testSome() {
let someMoreThanTwo
= this.arr
.some(this.checkMoreThanTwo
);
this.message
= someMoreThanTwo
;
},
testEvery() {
let allMoreThanTwo
= this.arr
.every(this.checkMoreThanTwo
);
this.message
= allMoreThanTwo
;
},
testForEach() {
this.arr
.forEach(function (item
, index
, input
) {
input
[index
] = item
* 2;
})
this.message
= this.arr
;
}
}
});
转载请注明原文地址:https://tech.qufami.com/read-690.html