• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Yancy00

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

js-数组相关方法

Array.prototype.map()

示例代码
//将创建一个新数组, 由原数组中每个元素都调用一次提供的函数后的返回值组成.
//callbackFunc 是数组中的每个元素执行的函数,它的返回值作为一个元素被添加到新数组中。
// callbackFunc函数被每次循环调用时将传入以下参数:
//- item 数组中当前正在处理的元素。
//- index 正在处理的元素在数组中的索引。
//- array  调用了 `map()` 的数组本身。
// 返回值: 一个新数组,每个元素都是回调函数的返回值。

const arr = [1, 4, 9, 16];
const resArr = arr.map((x) => x * 2);
console.log(resArr); //结果: [2, 8, 18, 32]

Array.prototype.some()

示例代码
// 数组相关方法----Array.prototype.some() 为数组中的每个元素执行func函数
//func函数的参数: item当前循环的元素,index当前循环的索引,array当前调用some的数组本身
//判断数组中是否至少有一个元素是偶数
const array = [1, 2, 1, 3, 5];
// const func = (e) => e % 2 === 0;
const res = array.some(e => e % 2 === 0);
console.log(res);

Array.prototype.every()

示例代码
//数组相关方法----Array.prototype.every()
// 检测数组内所有元素是否都能通过指定函数的测试,返回一个布尔值
const array1 = [1, 30, 39, 29, 10, 13];
//循环检测array1中每个元素是否都小于40
const res=array1.every( item=> item<40 );
console.log(res); //true

posted on 2023-10-01 07:28  Yancy00  阅读(22)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3