先端学習ノート(三)Javascript文字列関数配列関数
35994 ワード
Javascript文字列と配列の常用関数のまとめ 1.文字列関数 配列関数 1.文字列関数
/*
0, 1
*/
testCharAt() {
const str = "Hello World!";
let a = str.charAt(0);//H
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);//Hello
console.log(b);//World
console.log(c);//Hello World
},
// 。
// -1
testIndexOf() {
let a = "Hello World";
console.log(a.indexOf("ll"));//2
console.log(a.indexOf(" "));//5
console.log(a.indexOf("Wor"));//6
console.log(a.indexOf("wor"));//-1
console.log(a.indexOf("www"));//-1
},
//
testIncludes() {
let a = "Hello World";
console.log(a.includes("Wor"));//true
console.log(a.includes("wor"));//false
},
testLastIndexOf() {
let a = "Hello World";
console.log(a.lastIndexOf("Wor"));//6
console.log(a.lastIndexOf("wor"));//-1
},
testReplace() {
let a = "Hello World";
console.log(a.replace("World", "Pigg"));//Hello Pigg
},
//slice(start, end) , 。
// start( ) end( ) 。
// 0, 1, 。
testSlice() {
let a = "Hello World";
let b = a.slice(1, 3);
console.log(a);//Hello World
console.log(b);//el
},
//
testSplit() {
let a = "Hello World";
let b = a.split(" ");
console.log(a);//Hello World
console.log(b);//["Hello", "World"]
},
testStartsWith() {
let a = "Hello World";
console.log(a.startsWith("Hel"));//true
console.log(a.startsWith("hel"));//false
},
//substr() 。
//statr 。 。
// length 。 。 。 , stringObject 。
testSubstr() {
let a = "Hello World";
let b = a.substr(1, 3);
let c = a.substr(1);
console.log(a);//Hello World
console.log(b);//ell
console.log(c);//ello World
},
//substring() 。
//substring() , 。
testSubstring() {
let a = "Hello World";
let b = a.substring(1, 3);
let c = a.substring(1);
console.log(a);//Hello World
console.log(b);//el
console.log(c);//ello World
}
2配列関数 let vm = new Vue({
el: "#app1",
data: {
arr: [1, 2, 3, 4],
message: "hello vue.js!!!"
},
methods: {
checkMoreThanTwo(num) {
return num > 2;
},
//concat() 。
// , 。
testContact() {
let a = ["a", "aa"];
let b = ["b", "bb"];
let c = a.concat(b);
console.log(a);//["a", "aa"]
console.log(b);//["b", "bb"]
console.log(c);//["a", "aa", "b", "bb"]
},
//
testIncludes() {
let a = ["a", "aa"];
console.log(a.includes("a"));//true
console.log(a.includes("aaa"));//false
},
// , 。
//map ,
//
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);//["a"]
console.log(b);//aa
},
//push() ,
testPush() {
let a = ["a", "aa"];
let b = a.push("aaa", "aaaa");
console.log(a);//["a", "aa", "aaa", "aaaa"]
console.log(b);//4
},
testFind() {
//find
let arr2 = this.arr.find((n) => n > 1);
this.message = arr2;
},
testFindIndex() {
//findIndex ,index 0
let index = this.arr.findIndex((n) => n > 0);
this.message = index;
},
testFilter() {
//filter
let filterArr = this.arr.filter((n) => n > 3);
this.message = filterArr;
},
testSome() {
// true
let someMoreThanTwo = this.arr.some(this.checkMoreThanTwo);
this.message = someMoreThanTwo;
},
testEvery() {
// true
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;
}
}
});