关于js中数组的方法使用二

11. indexOf() 是 JavaScript 数组的方法,用于查找数组中某个特定元素的第一个匹配项,并返回其在数组中的索引。如果数组中不存在该元素,则返回 -1

const array = [1, 2, 3, 3, 4, 4, 5];

// 查找值为 3 的元素在数组中的索引
const index = array.indexOf(3);

console.log(index); // 输出: 2,因为值为 3 的元素第一次出现在索引 2 处

在这个例子中,indexOf() 方法返回了值为 3 的元素在数组中第一次出现的索引,即索引 2

如果数组中不存在要查找的元素,则返回 -1

const array = [1, 2, 3, 3, 4, 4, 5];

// 查找值为 6 的元素在数组中的索引
const index = array.indexOf(6);

console.log(index); // 输出: -1,因为值为 6 的元素并不在数组中

12. lastIndexOf() 是 JavaScript 数组的方法,类似于 indexOf(),但是它是从数组的末尾开始搜索指定元素的最后一个匹配项,并返回其在数组中的索引。如果数组中不存在该元素,则返回 -1

const array = [1, 2, 3, 3, 4, 4, 5];

// 查找值为 3 的元素在数组中的最后一个索引
const lastIndex = array.lastIndexOf(3);

console.log(lastIndex); // 输出: 3,因为值为 3 的元素最后一次出现在索引 3 处

在这个例子中,lastIndexOf() 方法返回了值为 3 的元素在数组中最后一次出现的索引,即索引 3

如果数组中不存在要查找的元素,则返回 -1

const array = [1, 2, 3, 3, 4, 4, 5];

// 查找值为 6 的元素在数组中的最后一个索引
const lastIndex = array.lastIndexOf(6);

console.log(lastIndex); // 输出: -1,因为值为 6 的元素并不在数组中

13. isArray() 是 JavaScript 的一个全局函数,用于检测一个值是否为数组类型。它返回一个布尔值,如果传入的参数是一个数组,则返回 true,否则返回 false

const array = [1, 2, 3];

// 检测一个值是否为数组类型
const isArray = Array.isArray(array);

console.log(isArray); // 输出: true,因为 array 是一个数组

14. join() 是 JavaScript 数组的方法,用于将数组的所有元素连接成一个字符串。

它接受一个可选的参数,这个参数表示连接数组元素时要使用的分隔符,默认情况下使用逗号作为分隔符。

//默认
const array = ['apple', 'banana', 'orange'];

// 将数组元素连接成一个字符串,使用逗号作为默认分隔符
const joinedString = array.join();

console.log(joinedString); // 输出: 'apple,banana,orange'


//传递一个自定义的分隔符作为参数
const array = ['apple', 'banana', 'orange'];

// 将数组元素连接成一个字符串,使用横线作为分隔符
const joinedString = array.join('-');

console.log(joinedString); // 输出: 'apple-banana-orange'

15. keys() 是 JavaScript 数组的方法,用于返回一个包含数组中每个索引键的迭代器对象。

const array = ['a', 'b', 'c'];

// 获取数组中每个索引键的迭代器对象
const iterator = array.keys();

for (const key of iterator) {
  console.log(key);
}

// 输出:
// 0
// 1
// 2

16. map() 是 JavaScript 数组的一个高阶函数,用于对数组中的每个元素执行指定操作,并返回一个新的数组,新数组包含每个元素执行操作后的结果。

const array = [1, 2, 3, 4, 5];

// 对数组中的每个元素执行操作,并生成一个新的数组
const newArray = array.map(element => element * 2);

console.log(newArray); // 输出: [2, 4, 6, 8, 10]

在这个例子中,map() 方法会对原始数组中的每个元素执行回调函数中定义的操作(这里是将每个元素乘以 2),然后将操作后的结果组成一个新的数组 newArray 返回。

map() 方法是非常有用的,它可以方便地将数组中的每个元素映射为另一种形式,例如对数组中的数值进行处理、生成新的对象数组等。

17. push() 是 JavaScript 数组的方法之一,用于向数组的末尾添加一个或多个元素,并返回修改后数组的新长度。

const array = [1, 2, 3];

// 向数组末尾添加一个元素
const newLength = array.push(4);

console.log(newLength); // 输出: 4,因为添加后数组的长度变为 4
console.log(array); // 输出: [1, 2, 3, 4],因为原始数组被修改,新增了元素 4
const array = [{ name: 'Alice' }, { name: 'Bob' }];

// 向数组末尾添加一个对象
array.push({ name: 'Charlie' });

console.log(array); 
// 输出: [{ name: 'Alice' }, { name: 'Bob' }, { name: 'Charlie' }]

18. pop() 是 JavaScript 数组的方法之一,用于移除数组中的最后一个元素,并返回被移除的元素值。这个方法会改变原始数组。

const array = [1, 2, 3, 4, 5];

// 移除数组中的最后一个元素
const removedElement = array.pop();

console.log(removedElement); // 输出: 5,因为被移除的是数组中的最后一个元素
console.log(array); // 输出: [1, 2, 3, 4],因为原始数组被修改,最后一个元素被移除

19. reduce() 是 JavaScript 数组的一个高阶方法,它对数组中的每个元素执行一个指定的回调函数,并将结果汇总为单个值。它是从数组的左边开始执行操作(从左向右)

const array = [1, 2, 3, 4, 5];

// 使用 reduce() 方法对数组元素求和
const sum = array.reduce(
    (accumulator, currentValue) => {
        accumulator + currentValue, 0
    }
);

console.log(sum); // 输出: 15,因为数组元素 1+2+3+4+5=15
//0 这里的0是累加器的初始值

reduce() 方法接受一个回调函数作为参数,这个函数会接收四个参数:

  • accumulator:累加器,存储回调函数的返回值。
  • currentValue:当前正在处理的元素。
  • currentIndex(可选):当前正在处理的元素索引。
  • array(可选):数组本身。

reduce() 方法还接受一个初始值参数作为可选参数。在上面的例子中,初始值是 0

20. reduceRight() 它是从数组的右边开始执行操作,向左遍历数组。(从右向左)它从数组的最后一个元素开始,向前依次处理到第一个元素。

const array = [1, 2, 3, 4, 5];

// 使用 reduceRight() 方法对数组元素求和
const sum = array.reduceRight(
    (accumulator, currentValue) => {
         accumulator + currentValue, 0
    }
);

console.log(sum); // 输出: 15,因为数组元素 5+4+3+2+1=15

具体用法和上面的19. reduce()相同,只是执行顺序不同。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值