JavaScript数组与字符串操作

JavaScript数组与字符串操作

JavaScript 数组操作方法

合并和连接
  • concat(): 合并两个或多个数组,返回新数组。
    const arr1 = [1, 2];
    const arr2 = [3, 4];
    const result = arr1.concat(arr2); // [1, 2, 3, 4]
    
查找元素
  • find(): 返回满足测试函数的第一个元素的值,找不到返回 undefined
    const arr = [1, 2, 3];
    const found = arr.find(element => element > 2); // 3
    
  • findIndex(): 返回满足测试函数的第一个元素的索引,找不到返回 -1
    const arr = [1, 2, 3];
    const index = arr.findIndex(element => element > 2); // 2
    
包含和索引
  • includes(): 判断数组是否包含指定值,返回 truefalse
    const arr = [1, 2, 3];
    const hasTwo = arr.includes(2); // true
    
  • indexOf(): 返回指定元素的第一个索引,找不到返回 -1
    const arr = [1, 2, 3];
    const index = arr.indexOf(2); // 1
    
转换和连接
  • join(): 将数组元素连接成一个字符串,默认用逗号分隔。
    const arr = [1, 2, 3];
    const str = arr.join('-'); // "1-2-3"
    
修改数组
  • pop(): 删除并返回数组最后一个元素,改变数组长度。
    const arr = [1, 2, 3];
    const last = arr.pop(); // 3, arr 变成 [1, 2]
    
  • push(): 添加一个或多个元素到数组末尾,返回新长度。
    const arr = [1, 2];
    const newLength = arr.push(3); // 3, arr 变成 [1, 2, 3]
    
  • shift(): 删除并返回数组第一个元素,改变数组长度。
    const arr = [1, 2, 3];
    const first = arr.shift(); // 1, arr 变成 [2, 3]
    
  • unshift(): 添加一个或多个元素到数组开头,返回新长度。
    const arr = [2, 3];
    const newLength = arr.unshift(1); // 3, arr 变成 [1, 2, 3]
    
  • splice(): 通过删除或替换元素来修改数组,返回被删除的元素。
    const arr = [1, 2, 3, 4];
    const removed = arr.splice(1, 2); // [2, 3], arr 变成 [1, 4]
    
反转和排序
  • reverse(): 反转数组元素顺序,改变原数组。
    const arr = [1, 2, 3];
    arr.reverse(); // [3, 2, 1]
    
  • sort(): 排序数组元素,默认按UTF-16代码单元值排序,改变原数组。
    const arr = [3, 1, 2];
    arr.sort(); // [1, 2, 3]
    

JavaScript 数组遍历方法

1. every()

every() 方法测试数组内的所有元素是否都能通过某个指定函数的测试。返回一个布尔值。

const arr = [1, 2, 3, 4];
const allEven = arr.every(num => num % 2 === 0); // false
2. filter()

filter() 方法创建一个新数组,包含所有通过所提供函数实现的测试的元素。

const arr = [1, 2, 3, 4];
const evenNumbers = arr.filter(num => num % 2 === 0); // [2, 4]
3. forEach()

forEach() 方法对数组的每个元素执行一次提供的函数。

const arr = [1, 2, 3, 4];
arr.forEach(num => console.log(num)); // 1, 2, 3, 4
4. some()

some() 方法测试是否至少有一个元素通过所提供的函数。返回一个布尔值。

const arr = [1, 2, 3, 4];
const hasEven = arr.some(num => num % 2 === 0); // true

JavaScript 字符串方法

1. charAt()

charAt() 方法返回指定索引位置的字符。

const str = "Hello";
const char = str.charAt(1); // "e"
2. concat()

concat() 方法将一个或多个字符串与原字符串连接,返回新字符串。

const str1 = "Hello";
const str2 = "World";
const result = str1.concat(" ", str2); // "Hello World"
3. includes()

includes() 方法判断一个字符串是否包含另一个字符串,返回 truefalse

const str = "Hello World";
const hasHello = str.includes("Hello"); // true
4. indexOf()

indexOf() 方法返回指定值在字符串中第一次出现的位置,未找到返回 -1

const str = "Hello World";
const index = str.indexOf("World"); // 6
5. match()

match() 方法检索字符串并返回匹配正则表达式的结果。

const str = "Hello 123";
const result = str.match(/\d+/); // ["123"]
6. padStart()

padStart() 方法用另一个字符串填充当前字符串(从左侧开始),直到达到给定长度。

const str = "5";
const padded = str.padStart(2, '0'); // "05"
7. replace()

replace() 方法返回一个新字符串,其中某些模式(字符串或正则表达式)被替换。

const str = "Hello World";
const newStr = str.replace("World", "Everyone"); // "Hello Everyone"
8. slice()

slice() 方法提取字符串的一部分,并返回一个新的字符串。

const str = "Hello World";
const sliced = str.slice(0, 5); // "Hello"
9. split()

split() 方法使用指定的分隔符将字符串分割成数组。

const str = "Hello World";
const arr = str.split(" "); // ["Hello", "World"]
10. substr()

substr() 方法返回字符串中从指定位置开始到指定字符数的字符。

const str = "Hello World";
const sub = str.substr(0, 5); // "Hello"
11. trim()

trim() 方法去除字符串两端的空白字符。

const str = "  Hello World  ";
const trimmed = str.trim(); // "Hello World"
  • 24
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爪洼守门员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值