前端理论总结(js)——数组操作方法

1:map:通过指定的函数处理数组的每个元素 然后返回处理后的数组

const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2); // [2, 4, 6, 8]

2:push:尾部添加

const fruits = ['apple', 'banana'];
fruits.push('orange'); // ['apple', 'banana', 'orange']


3:pop:尾部删除

const numbers = [1, 2, 3, 4];
numbers.pop(); // [1, 2, 3]

4:shift:头部删除

const numbers = [1, 2, 3, 4];
numbers.shift(); // [2, 3, 4]

5:unshift:头部添加

const fruits = ['apple', 'banana'];
fruits.unshift('orange'); // ['orange', 'apple', 'banana']

6:reverse:颠倒

const numbers = [1, 2, 3, 4];
numbers.reverse(); // [4, 3, 2, 1]

7:concat:合并

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const combined = array1.concat(array2); // [1, 2, 3, 4, 5, 6]

8:join:转换成字符串

const fruits = ['apple', 'banana', 'cherry'];
const result = fruits.join(', '); // "apple, banana, cherry"

9:toString:转字符串

const numbers = [1, 2, 3];
const result = numbers.toString(); // "1,2,3"

10:JSON.stringify:转换成JSON数据

const person = { name: 'John', age: 30 };
const jsonString = JSON.stringify(person); // '{"name":"John","age":30}'

11:forEach:遍历数组

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

12:filter:过滤数组

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

13:some:数组求和

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

14:split:以指定字符分割数组,返回新数组

const str = "apple,banana,cherry";
const fruits = str.split(','); // ['apple', 'banana', 'cherry']

15:splice:截取,2个参数:删除  3个参数:添加(开始,删除数量,插入数量)

  • 删除元素:
const fruits = ['apple', 'banana', 'cherry'];
fruits.splice(1, 1); // 删除索引为1的元素,结果:['apple', 'cherry']
  • 添加元素:
const fruits = ['apple', 'cherry'];
fruits.splice(1, 0, 'banana'); // ['apple', 'banana', 'cherry']

16:slice:截取(开始位置,数量)

const numbers = [1, 2, 3, 4, 5];
const sliceResult = numbers.slice(1, 4); // [2, 3, 4]

17:sort:排序

const numbers = [4, 1, 3, 2];
numbers.sort(); // [1, 2, 3, 4]

ヾ( ̄▽ ̄)Bye~Bye~

ጿ ኈ ቼ ዽ ጿ ኈ ቼ ዽ ጿ ኈ ቼ ዽ ጿ ኈ ቼ ዽ ጿ ኈ ቼ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值