归纳JavaScript中的数组与字符串常用内置方法

JavaScript中的数组与字符串常用内置方法

数组(15个)

  1. push() 方法将一个或多个元素添加到数组的末尾,并返回新数组的长度。示例如下
const fruits = ['apple', 'banana'];
const newLength = fruits.push('orange');
console.log(fruits); // ['apple', 'banana', 'orange']
console.log(newLength); // 3

  1. pop() 方法从数组的末尾移除并返回最后一个元素。示例如下
const fruits = ['apple', 'banana', 'orange'];
const lastFruit = fruits.pop();
console.log(fruits); // ['apple', 'banana']
console.log(lastFruit); // 'orange'

  1. shift() 方法从数组的开头移除并返回第一个元素。 示例如下
const fruits = ['apple', 'banana', 'orange'];
const firstFruit = fruits.shift();
console.log(fruits); // ['banana', 'orange']
console.log(firstFruit); // 'apple'

  1. unshift() 方法向数组的开头添加一个或多个元素,并返回新数组的长度注意是长度。示例如下
const fruits = ['banana', 'orange'];
const newLength = fruits.unshift('apple');
console.log(fruits); // ['apple', 'banana', 'orange']
console.log(newLength); // 3

  1. concat() 方法用于合并两个或多个数组,并返回一个新数组。示例如下
const fruits1 = ['apple', 'banana'];
const fruits2 = ['orange', 'grape'];
const mergedFruits = fruits1.concat(fruits2);
console.log(mergedFruits); // ['apple', 'banana', 'orange', 'grape']

  1. slice() 方法返回数组的一部分,从开始索引到结束索引(不包括结束索引),并创建一个新数组。示例如下
const fruits = ['apple', 'banana', 'orange', 'grape'];
const slicedFruits = fruits.slice(1, 3);
console.log(slicedFruits); // ['banana', 'orange']

  1. 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']

  1. join() 方法将数组中的所有元素连接成一个字符串,并返回该字符串。示例如下
const fruits = ['apple', 'banana', 'orange'];
const joinedString = fruits.join(', ');
console.log(joinedString); // 'apple, banana, orange'

  1. indexOf() 方法返回指定元素在数组中首次出现的索引,如果不存在则返回 -1。示例如下
const fruits = ['apple', 'banana', 'orange'];
const bananaIndex = fruits.indexOf('banana');
console.log(bananaIndex); // 1

  1. lastIndexOf() 方法返回指定元素在数组中最后一次出现的索引,如果不存在则返回 -1。示例如下
const fruits = ['apple', 'banana', 'orange', 'banana'];
const lastBananaIndex = fruits.lastIndexOf('banana');// banana出现了两次
console.log(lastBananaIndex); // 3

  1. includes() 方法判断数组是否包含指定元素,返回布尔值。示例如下
const fruits = ['apple', 'banana', 'orange'];
const hasBanana = fruits.includes('banana');
console.log(hasBanana); // true

  1. forEach() 方法对数组中的每个元素执行提供的函数。通过使用 forEach() 方法,我们可以方便地对数组中的每个元素进行操作,而不需要使用传统的 for 循环。 示例如下
const fruits = ['apple', 'banana', 'orange'];
fruits.forEach((fruit) => { // 箭头函数,接收参数fruit
  console.log(fruit);
});
// Output(控制台输出如下):
// 'apple'
// 'banana'
// 'orange'

  1. map() 方法创建一个新数组,其结果是对原数组中的每个元素应用提供的函数。示例如下
const numbers = [1, 2, 3];
const doubledNumbers = numbers.map((number) => {
  return number * 2;
});
console.log(doubledNumbers); // [2, 4, 6]

  1. filter() 方法创建一个新数组,其中包含通过提供函数的测试的所有元素。示例如下
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter((number) => {
  return number % 2 === 0;
});
console.log(evenNumbers); // [2, 4]

  1. reduce() 方法将数组中的元素通过提供的函数进行累积。示例如下
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => {
  return accumulator + currentValue;
}, 0);
console.log(sum); // 15

字符串(9个)

  1. length 属性返回字符串的长度。示例如下
const str = 'Hello, world!';
console.log(str.length); // 13,分别是
'H'
'e'
'l'
'l'
'o'
','
' ' // 这是空格
'w'
'o'
'r'
'l'
'd'
'!'

  1. charAt() 方法返回指定索引位置的字符。示例如下
const str = 'Hello, world!';
const char = str.charAt(7);
console.log(char); // 'w'

  1. concat() 方法将两个或多个字符串连接起来,返回一个新字符串。示例如下
const str1 = 'Hello, ';
const str2 = 'world!';
const concatenatedStr = str1.concat(str2);
console.log(concatenatedStr); // 'Hello, world!'

  1. substring() 方法返回位于两个索引之间的子字符串,不包括结束索引。示例如下
const str = 'Hello, world!';
const substring = str.substring(7, 12);
console.log(substring); // 'world'

  1. 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'

  1. toUpperCase() 方法将字符串转换为大写字母形式。示例如下
const str = 'Hello, world!';
const uppercaseStr = str.toUpperCase();
console.log(uppercaseStr); // 'HELLO, WORLD!'

  1. toLowerCase() 方法将字符串转换为小写字母形式。示例如下
const str = 'Hello, world!';
const lowercaseStr = str.toLowerCase();
console.log(lowercaseStr); // 'hello, world!'

  1. split() 方法将字符串拆分为子字符串数组,基于指定的分隔符。示例如下
const str = 'Hello, world!';
const words = str.split(' ');
console.log(words); // ['Hello,', 'world!']

  1. trim() 方法删除字符串两端的空白字符,并返回新字符串。示例如下
const str = '   Hello, world!   ';
const trimmedStr = str.trim();
console.log(trimmedStr); // 'Hello, world!'

好啦,今日的分享就到这里了,希望对你有帮助,谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佳辰辰辰辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值