数组(15个)
- push() 方法将一个或多个元素添加到数组的末尾,并返回新数组的长度。示例如下
const fruits = ['apple', 'banana'];
const newLength = fruits.push('orange');
console.log(fruits); // ['apple', 'banana', 'orange']
console.log(newLength); // 3
- pop() 方法从数组的末尾移除并返回最后一个元素。示例如下
const fruits = ['apple', 'banana', 'orange'];
const lastFruit = fruits.pop();
console.log(fruits); // ['apple', 'banana']
console.log(lastFruit); // 'orange'
- shift() 方法从数组的开头移除并返回第一个元素。 示例如下
const fruits = ['apple', 'banana', 'orange'];
const firstFruit = fruits.shift();
console.log(fruits); // ['banana', 'orange']
console.log(firstFruit); // 'apple'
- unshift() 方法向数组的开头添加一个或多个元素,并返回新数组的长度,注意是长度。示例如下
const fruits = ['banana', 'orange'];
const newLength = fruits.unshift('apple');
console.log(fruits); // ['apple', 'banana', 'orange']
console.log(newLength); // 3
- concat() 方法用于合并两个或多个数组,并返回一个新数组。示例如下
const fruits1 = ['apple', 'banana'];
const fruits2 = ['orange', 'grape'];
const mergedFruits = fruits1.concat(fruits2);
console.log(mergedFruits); // ['apple', 'banana', 'orange', 'grape']
- slice() 方法返回数组的一部分,从开始索引到结束索引(不包括结束索引),并创建一个新数组。示例如下
const fruits = ['apple', 'banana', 'orange', 'grape'];
const slicedFruits = fruits.slice(1, 3);
console.log(slicedFruits); // ['banana', 'orange']
- splice() 方法可以删除、插入或替换数组的元素,并返回被删除的元素组成的数组。示例如下
const fruits = ['apple', 'banana', 'orange', 'grape'];
const removedFruits = fruits.splice(1, 2, 'mango', 'pear');
console.log(fruits); // ['apple', 'mango', 'pear', 'grape']
console.log(removedFruits); // ['banana', 'orange']
- join() 方法将数组中的所有元素连接成一个字符串,并返回该字符串。示例如下
const fruits = ['apple', 'banana', 'orange'];
const joinedString = fruits.join(', ');
console.log(joinedString); // 'apple, banana, orange'
- indexOf() 方法返回指定元素在数组中首次出现的索引,如果不存在则返回 -1。示例如下
const fruits = ['apple', 'banana', 'orange'];
const bananaIndex = fruits.indexOf('banana');
console.log(bananaIndex); // 1
- lastIndexOf() 方法返回指定元素在数组中最后一次出现的索引,如果不存在则返回 -1。示例如下
const fruits = ['apple', 'banana', 'orange', 'banana'];
const lastBananaIndex = fruits.lastIndexOf('banana');// banana出现了两次
console.log(lastBananaIndex); // 3
- includes() 方法判断数组是否包含指定元素,返回布尔值。示例如下
const fruits = ['apple', 'banana', 'orange'];
const hasBanana = fruits.includes('banana');
console.log(hasBanana); // true
- forEach() 方法对数组中的每个元素执行提供的函数。通过使用 forEach() 方法,我们可以方便地对数组中的每个元素进行操作,而不需要使用传统的 for 循环。 示例如下
const fruits = ['apple', 'banana', 'orange'];
fruits.forEach((fruit) => { // 箭头函数,接收参数fruit
console.log(fruit);
});
// Output(控制台输出如下):
// 'apple'
// 'banana'
// 'orange'
- map() 方法创建一个新数组,其结果是对原数组中的每个元素应用提供的函数。示例如下
const numbers = [1, 2, 3];
const doubledNumbers = numbers.map((number) => {
return number * 2;
});
console.log(doubledNumbers); // [2, 4, 6]
- filter() 方法创建一个新数组,其中包含通过提供函数的测试的所有元素。示例如下
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter((number) => {
return number % 2 === 0;
});
console.log(evenNumbers); // [2, 4]
- reduce() 方法将数组中的元素通过提供的函数进行累积。示例如下
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
console.log(sum); // 15
字符串(9个)
- length 属性返回字符串的长度。示例如下
const str = 'Hello, world!';
console.log(str.length); // 13,分别是
'H'
'e'
'l'
'l'
'o'
','
' ' // 这是空格
'w'
'o'
'r'
'l'
'd'
'!'
- charAt() 方法返回指定索引位置的字符。示例如下
const str = 'Hello, world!';
const char = str.charAt(7);
console.log(char); // 'w'
- concat() 方法将两个或多个字符串连接起来,返回一个新字符串。示例如下
const str1 = 'Hello, ';
const str2 = 'world!';
const concatenatedStr = str1.concat(str2);
console.log(concatenatedStr); // 'Hello, world!'
- substring() 方法返回位于两个索引之间的子字符串,不包括结束索引。示例如下
const str = 'Hello, world!';
const substring = str.substring(7, 12);
console.log(substring); // 'world'
- slice() 方法返回位于两个索引之间的子字符串,不包括结束索引。 与 substring() 不同的是,slice() 允许使用负数作为参数,表示从字符串末尾开始计数的索引位置。而substring() 方法在处理负数索引时会将其转换为 0,因此 substring(-3, -1) 等同于 substring(0, 0)。 示例如下
const str = 'Hello, world!';
const slicedStr = str.slice(-5, -1); // 从倒数第 5 个字符开始提取,直到倒数第 1 个字符之前(不包括倒数第 1 个字符)
console.log(slicedStr); // 'orld'
- toUpperCase() 方法将字符串转换为大写字母形式。示例如下
const str = 'Hello, world!';
const uppercaseStr = str.toUpperCase();
console.log(uppercaseStr); // 'HELLO, WORLD!'
- toLowerCase() 方法将字符串转换为小写字母形式。示例如下
const str = 'Hello, world!';
const lowercaseStr = str.toLowerCase();
console.log(lowercaseStr); // 'hello, world!'
- split() 方法将字符串拆分为子字符串数组,基于指定的分隔符。示例如下
const str = 'Hello, world!';
const words = str.split(' ');
console.log(words); // ['Hello,', 'world!']
- trim() 方法删除字符串两端的空白字符,并返回新字符串。示例如下
const str = ' Hello, world! ';
const trimmedStr = str.trim();
console.log(trimmedStr); // 'Hello, world!'
好啦,今日的分享就到这里了,希望对你有帮助,谢谢!!!